Giter VIP home page Giter VIP logo

catacli's Introduction

catacli

Super TypeScript-friendly commander helper.

Build Status npm version Greenkeeper badge

install

$ npm install catacli

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 "catacli";

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 (short-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: "catacli 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 - catacli 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 "catacli";

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: "catacli 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: "catacli 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: "catacli 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 - catacli 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

catacli's People

Contributors

akito0107 avatar greenkeeper[bot] avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

wade1990 kt3k

catacli's Issues

Subcommand does not work.

I tried to create subcommand, using same code as README subcommand example.
There is build error.

ERROR in /path/to/repo/src/index.ts
./src/index.ts
[tsl] ERROR in /path/to/repo/src/index.ts(73,3)
      TS2322: Type '(args: any, _: any, helpFn: any, { spec, rawArgs }: { spec: any; rawArgs: any; }) => void' is not assignable to type '(args: { COMMAND_NAME: { value: string; position: number[]; usage: string; }; }, flags: ParseResult<boolean, "opts1"> & ParseResult<number, "opts2"> & ParseResult<string, "opts3">, helpFn?: Function | undefined, metaInfo?: { ...; } | undefined) => any'.
  Types of parameters '__3' and 'metaInfo' are incompatible.
    Type '{ spec?: CommandSpec<"example", (args: string[]) => ParseResult<boolean, "opts1"> & ParseResult<number, "opts2"> & ParseResult<string, "opts3">, (args: string[]) => { ...; }> | undefined; rawArgs?: string[] | undefined; } | undefined' is not assignable to type '{ spec: any; rawArgs: any; }'.
      Type 'undefined' is not assignable to type '{ spec: any; rawArgs: any; }'.

Is there any problem on subcommand example?

$node -v
v14.9.0
$grep catacli package.json 
    "catacli": "^0.1.3",

can't create no flag commands.

I tried create simple command that uses only arguments (like a file path).
But if do not assignment flag property for makeCommand function 2nd arguments, get an Error: TypeError: t2 is not a function

I did investigation for this.

makeCommand option of flag is Partial type intype CommandSpec.

catacli/src/command.ts

Lines 5 to 26 in 14d54bd

export type CommandSpec<
N extends string,
F extends (args: string[]) => any,
P extends (args: string[]) => any
> = {
name: N;
description?: string;
version?: string;
usage?: string;
flag?: F;
positionalArguments?: P;
handler?: F extends (args: string[]) => infer V
? P extends (args: string[]) => infer U
? (
args: U,
flags: V,
helpFn?: Function,
metaInfo?: { spec?: CommandSpec<N, F, P>; rawArgs?: string[] }
) => any
: never
: never;
};

But inside of makeCommand function, spec.flag is calling as a function.

catacli/src/command.ts

Lines 45 to 54 in 14d54bd

export function makeCommand<
N extends string,
T extends (args: string[]) => any,
P extends (args: string[]) => any
>(spec: CommandSpec<N, T, P>, showHelp = defaultHelp): Command {
return (args: string[], parentSpec) => {
const parser = showHelp
? reduceFlag(defaultHelpFlag, spec.flag)
: spec.flag;
const flags = parser(args);

In short, if do not assignment flag property, spec.flag is undefined but called as as function.
then get an Error: TypeError: t2 is not a function.

that is correct?
and if correct this, may i try create a pull request?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency @types/node to v20.14.9
  • Update actions/checkout action to v4
  • Update actions/setup-node action to v4
  • Update dependency eslint-config-prettier to v9
  • Lock file maintenance
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/pr.yml
  • actions/checkout v1
  • actions/setup-node v1
npm
package.json
  • source-map-support ^0.5.12
  • @types/jest 27.5.2
  • @types/node 20.12.11
  • @typescript-eslint/eslint-plugin 1.13.0
  • @typescript-eslint/parser 1.13.0
  • eslint 7.32.0
  • eslint-config-prettier 8.10.0
  • eslint-plugin-prettier 4.0.0
  • jest 27.5.1
  • prettier 1.19.1
  • rimraf 3.0.2
  • ts-jest 27.1.4
  • ts-node 9.1.1
  • typescript 4.9.5

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.