Skip to content
No description or website provided.
Branch: master
Clone or download
Latest commit ccc5ec2 Jun 25, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src refactor: compose to reduce Jun 24, 2019
.eslintrc.js feat: impl flag Jun 5, 2019
.gitignore fix ignore Jun 4, 2019
.npmignore fix: add npmignore Jun 14, 2019
LICENSE feat; add readme Jun 7, 2019
README.md refactor: compose to reduce Jun 24, 2019
azure-pipelines.yml update pipelines Jun 22, 2019
package.json v0.0.3 Jun 24, 2019
tsconfig.json feat: add subcommand handler Jun 13, 2019
yarn.lock fix: lint Jun 22, 2019

README.md

marron-glace

Super TypeScript-friendly commander helper.

Build Status npm version Greenkeeper badge

install

$ npm install marron-glace

usage

Simple Command

Using reduceFlag, makePositionalArguments and makeCommand, you can implements handler functions with typed args.

import {
  makeBooleanFlag,
  makeNumberFlag,
  makeStringFlag,
  makeCommand,
  reduceFlag,
  makeStringArgument,
  makePositionalArguments
} from "marron-glace";

const booleanFlag = makeBooleanFlag("opts1", {
  usage: "boolean example"
});
const numberFlag = makeNumberFlag("opts2", {
  default: 1, // you can pass default value,
  usage: "number example"
});
const stringFlag = makeStringFlag("opts3", {
  alias: "a", // you can specify alias (shot-hand flag)
  usage: "string example"
});

const stringArg = makeStringArgument("arg1");

const flags = reduceFlag(booleanFlag, numberFlag, stringFlag);
const args = makePositionalArguments(stringArg);

const command = makeCommand({
  name: "example",
  description: "marron-glace is typescript-friendly commander tool",
  version: "0.0.1",
  usage: "simple [OPTIONS] arg1",
  flag: flags,
  positionalArguments: args,
  handler: (args, opts) => {
    /* YOUR COMMAND LOGIC IS HERE */
    console.log("positionalArgs: ", args.arg1.value); // ok and inferred as string type
    console.log("flag opts1: ", opts.opts1.value); // ok and inferred as boolean type
    console.log("flag opts2: ", opts.opts2.value); // ok and inferred as number type
    console.log("flag opts3: ", opts.opts3.value); // ok and inferred as string type
    // opts.arg4; // ng compile error
  }
});

command(process.argv.splice(2));

running with ts-node

$ ts-node main.ts --opts1 --opts2 123 --opts3 test args

and got these outputs.

positionalArgs:  args
flag opts1:  true
flag opts2:  123
flag opts3:  test

Short-hand flag

--opts3 is also acceptable with -a flag.

% ts-node main.ts --opts1 --opts2 123 -a test args
positionalArgs:  args
flag opts1:  true
flag opts2:  123
flag opts3:  test

with Help

You can show rich help texts with --help flag by default.

$ ts-node main.ts --opts1 -a test --opts3 123 args --help
NAME:
   example - marron-glace is typescript-friendly commander tool

USAGE:
   simple [OPTIONS] arg1

VERSION:
  0.0.1

ARGUMENTS:
	 arg1

OPTIONS:
	--help  	 show help
	--opts1  	 boolean example
	--opts2  	 number example	default=1
	--opts3, -a  	 string example

SubCommands

You can create subcommnds with makeSubCommandHandler.

import {
  makeBooleanFlag,
  makeNumberFlag,
  makeStringFlag,
  makeCommand,
  reduceFlag,
  makeStringArgument,
  makePositionalArguments,
  makeSubCommandHandler,
  makeSubCommandNameArgument
} from "marron-glace";

const booleanFlag = makeBooleanFlag("opts1", {
  usage: "boolean example"
});
const numberFlag = makeNumberFlag("opts2", {
  default: 1, // you can pass default value,
  usage: "number example"
});
const stringFlag = makeStringFlag("opts3", {
  alias: "a", // you can specify alias value
  usage: "string example"
});

const flags = reduceFlag(booleanFlag, numberFlag, stringFlag);
const sub1Flag = reduceFlag(flags, makeStringFlag("subflag1"));

const stringArg = makeStringArgument("arg1");
const args = makePositionalArguments(stringArg);

const subCommand1 = makeCommand({
  name: "sub1",
  description: "marron-glace subcommand example (sub1)",
  version: "0.0.1",
  usage: "example [OPTIONS] sub1 [SUB COMMAND OPTIONS]",
  flag: sub1Flag,
  positionalArguments: args,
  handler: (args, flags) => {
    console.log("arg1: ", args.arg1.value);
    console.log("opts1: ", flags.opts1.value);
    console.log("opts2: ", flags.opts2.value);
    console.log("opts3: ", flags.opts3.value);
    console.log("subflag1: ", flags.subflag1.value); // inffered as a string type
  }
});

const sub2Flag = reduceFlag(flags, makeStringFlag("subflag2"));

const subCommand2 = makeCommand({
  name: "sub2",
  description: "marron-glace subcommand example (sub2)",
  version: "0.0.1",
  usage: "example [OPTIONS] sub2 [SUB COMMAND OPTIONS]",
  flag: sub2Flag,
  handler: (_, flags) => {
    console.log("subflag2: ", flags.subflag2.value);
  }
});

const commandNames = makePositionalArguments(
  makeSubCommandNameArgument("sub1", "sub2")
);

const command = makeCommand({
  name: "example",
  description: "marron-glace is typescript-friendly commander tool",
  version: "0.0.1",
  usage: "simple [OPTIONS] [COMMAND_NAME] [SUB COMMAND OPTIONS]",
  flag: flags,
  /* YOU MUST SPECIFY positionalArguments with `makeSubCommandNameArgument` */
  positionalArguments: commandNames /* YOU MUST SPECIFY positionalArguments with `makeSubCommandNameArgument` */,
  /* passing sub commands with commandName to `makeSubCommandHandler()` */
  handler: makeSubCommandHandler(
    { name: "sub1", command: subCommand1 },
    { name: "sub2", command: subCommand2 }
  )
});

command(process.argv.splice(2));

running with ts-node

$ ts-node main.ts  --opts1 --opts2 123 --opts3 test  sub1  --subflag1 test sub-positional-args
arg1:  sub-positional-args
opts1:  true
opts2:  123
opts3:  test
subflag1:  test

and also shows rich help texts with --help.

% ts-node main.ts  --opts1 --opts2 123 --opts3 test  sub1  --subflag1 test sub-positional-args --help

NAME:
   example sub1 - marron-glace subcommand example (sub1)

USAGE:
   example [OPTIONS] sub1 [SUB COMMAND OPTIONS]

VERSION:
  0.0.1

ARGUMENTS:
	 arg1

OPTIONS:
	--opts3, -a  	 string example
	--opts2  	 number example	default=1
	--opts1  	 boolean example

SUB OPTIONS:
	--subflag1
	--help  	 show help

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details

You can’t perform that action at this time.