Skip to content

Commit

Permalink
Improve arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Sep 5, 2019
1 parent 744dd5a commit 94a7c85
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions xeval/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ type Reader = Deno.Reader;
const AsyncFunction = Object.getPrototypeOf(async function(): Promise<void> {})
.constructor;

function showHelp(): void {
console.log(`Deno xeval
const HELP_MSG = `Deno xeval
USAGE:
deno -A https://deno.land/std/xeval/mod.ts [OPTIONS] <code>
Expand All @@ -18,9 +17,7 @@ OPTIONS:
-I, --replvar <replvar> Set variable name to be used in eval, defaults to $
ARGS:
<code>
`);
}
<code>`;

export type XevalFunc = (v: string) => void;

Expand Down Expand Up @@ -139,15 +136,24 @@ async function main(): Promise<void> {
delim: ["d"],
replvar: ["I"],
help: ["h"]
},
default: {
delim: DEFAULT_DELIMITER,
replvar: "$"
}
});

if (parsedArgs.help || parsedArgs._.length != 1) {
return showHelp();
if (parsedArgs._.length != 1) {
console.error(HELP_MSG);
exit(1);
}

if (parsedArgs.help) {
return console.log(HELP_MSG);
}

const delimiter = parsedArgs.delim || DEFAULT_DELIMITER;
const replVar = parsedArgs.replvar || "$";
const delimiter = parsedArgs.delim;
const replVar = parsedArgs.replvar;
const code = parsedArgs._[0];

// new AsyncFunction()'s error message for this particular case isn't great.
Expand Down

0 comments on commit 94a7c85

Please sign in to comment.