Skip to content

Commit

Permalink
fix argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
k3kaimu committed Jan 19, 2020
1 parent 650d5b0 commit 8f2b234
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
6 changes: 3 additions & 3 deletions examples/qsubxargs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

```
Usage:
qsubxargs <tuthpc-lib options...> <xargs options...> <commands...>
qsubxargs <tuthpc-lib options...> <xargs options...> -- <commands...>
Where:
<tuthpc-lib options...>: options for qsubxargs
<xargs options...>: options for xargs
<commands...>: commands
For example:
ls -1 | qsubxargs --th:g=28 --th:m=100 -l echo
$ ls -1 | qsubxargs --th:g=28 --th:m=100 -l -- echo
```


Expand Down Expand Up @@ -61,7 +61,7 @@ export TUTHPC_STARTUP_SCRIPT='source ~/.bashrc'
たとえば生成されたコマンドが4000個のとき,各ジョブは40個のコマンドを20並行で処理する.

```sh
qsubxargs --th:g=20 --th:m=100 <xargs options...> <commands...>
qsubxargs --th:g=20 --th:m=100 <xargs options...> -- <commands...>
```


Expand Down
68 changes: 43 additions & 25 deletions examples/qsubxargs/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,30 @@ bool flagDryRun = false; // ジョブを投入せずに,コマンドの
bool flagVerbose = false; // 冗長な出力を含む


string[] args_tuthpclib_options;
string[] args_xargs_options;
string[] args_commands;


void main(string[] args)
{
auto env = defaultJobEnvironment;
env.isEnabledRenameExeFile = false; // この実行ファイルをコピーしない
env.isEnabledQueueOverflowProtection = false; // ユーザーに全てを委ねる
env.isEnabledUserCheckBeforePush = false; // ユーザーに全てを委ねる

getopt(args,
std.getopt.config.passThrough,
"th:dryrun", &flagDryRun,
"th:verbose", &flagVerbose);

if(args.length == 1) {
printUsage();
return;
}

// プログラム名を無視
args = args[1 .. $];
parseArgs(args);

// "--th:"が頭についている引数は無視する
while(args.length && args[0].startsWith("--th:"))
args = args[1 .. $];

if(args.length == 0) {
if(args_commands.length == 0) {
printUsage();
return;
}


// ログ出力用のディレクトリ名
if(env.jobName) {
env.logdir = format("logs_%s", env.jobName);
Expand All @@ -64,7 +58,7 @@ void main(string[] args)
// logdir/scripts 以下にシェルスクリプトを保存する
if(thisProcessType() == ChildProcessType.SUBMITTER) {
// コマンドのリストを作成
string[] commands = makeCommandLines(args);
string[] commands = makeCommandLines();
if(flagDryRun) {
writefln("%-(%s\n%)", commands);
return;
Expand Down Expand Up @@ -95,23 +89,47 @@ void main(string[] args)

// ジョブの投入 or 実行
taskList.run(env);

// foreach(i; 0 .. taskList.length) taskList[i]();
}


string[] makeCommandLines(const(string)[] xargsOptions)
void parseArgs(string[] args)
{
string[] program = ["xargs"];
string[] dummy_args = args.dup;
getopt(dummy_args,
std.getopt.config.passThrough,
"th:dryrun", &flagDryRun,
"th:verbose", &flagVerbose);

// "-"が頭についている引数を追加する
while(xargsOptions.length && xargsOptions[0].startsWith("-")) {
program ~= xargsOptions[0];
xargsOptions = xargsOptions[1 .. $];
// プログラム名を無視
args = args[1 .. $];

// "--th:" が頭についている引数
while(args.length && args[0].startsWith("--th:")) {
args_tuthpclib_options ~= args[0];
args = args[1 .. $];
}

program ~= "echo";
program ~= xargsOptions; // 残りの引数を追加する
// -- が出現するまでは xargs のオプション
while(args.length && args[0] != "--") {
args_xargs_options ~= args[0];
args = args[1 .. $];
}

// -- がそもそも引数になかった場合
if(args.length == 0)
return;

// -- を消す
args = args[1 .. $];

// 残りはコマンド
args_commands ~= args;
}


string[] makeCommandLines()
{
string[] program = ["xargs"] ~ args_xargs_options ~ ["echo"] ~ args_commands;

auto stdoutPipe = pipe();
auto xargsPid = spawnProcess(program, stdin, stdoutPipe.writeEnd, stderr);
Expand Down Expand Up @@ -151,7 +169,7 @@ void printUsage()

immutable string strUsage = `
Usage:
qsubxargs <tuthpc-lib options...> <xargs options...> <commands...>
qsubxargs <tuthpc-lib options...> <xargs options...> -- <commands...>
Where:
<tuthpc-lib options...>: options for qsubxargs
Expand Down

0 comments on commit 8f2b234

Please sign in to comment.