1.1.1 • Published 5 years ago
@xornot/command-line v1.1.1
xornot.io command-line
Command line parser and usage helper.
Args
import {ArgsBuilder} from "command-line";
const args: Args = new ArgsBuilder()
.command("command") // Argument without a "-" prefix, before all "-" prefixed arguments.
.option("foo", "f") // Argument prefixed with a "-", with a value.
.flag("bar", "b") // Argument prefixed with a "-", without a value.
.positional("baz") // Argument without a "-" prefix or after a "--" argument, not used by another argument.
.get();
const command: string = args.command.last.value; // throws if there are no values.
const foo: string[] = args.foo.all; // All values (options can be used more than once).
const bar: boolean = args.bar.isPresent; // Flags have no values, they're just present or missing.
const baz: string = args.baz.last.or("default");
const unused: string[] = args.$argv; // All values not consumed by defined args.
// Re-open the args builder to add new args.
// * The new args will still have all values from the source args.
const newArgs: Args = args.$builder
.option("abc")
.get();
Usage
import {Usage} from "command-line";
// The least common indent is stripped from the beginning of all lines.
const usage = new Usage(`
Usage: tool [options...]
`);
// The following commands all print the usage text and exit immediately.
usage.show();
usage.error();
usage.error("Error message");