cac icon indicating copy to clipboard operation
cac copied to clipboard

[feature] sub commands like 'git remote prune origin'

Open trusktr opened this issue 4 years ago • 7 comments

Issue Type

  • [ ] Bug Report
  • [x] Feature Request
  • [ ] Other

There doesn't seem to be mention of this, so not sure if possible.

Example:

my-command remote rm origin

Maybe this is actually a feature request. I can change it if so.

trusktr avatar Feb 27 '20 18:02 trusktr

Only 1-level sub command is currently supported, but I'm interested in this feature, do you know any library that actually supports this?

egoist avatar Feb 27 '20 18:02 egoist

Yeah, there are a few CLI tools with subcommands features, but they are all written plain JS:

  • yargs
    • example with folder hierarchy: https://github.com/yargs/yargs/blob/master/docs/advanced.md#example-command-hierarchy-using-commanddir
    • example with nested functions: https://github.com/yargs/yargs/issues/225#issuecomment-128532719
  • Sywac is newer and inspired by Yargs (a bunch of parts of the documentation are WIP. @nexdrew)
    • Example with nested functions: http://sywac.io/docs/sync-config.html#command
    • Folder hierarchy: http://sywac.io/docs/sync-config.html#commandDirectory

trusktr avatar Feb 27 '20 21:02 trusktr

Here's a lazy solution until @egoist figures out how to support this.

https://github.com/aleclarson/cac/commit/227835ee2a2621626c2fa75393787258287aab95

const app = cac()

app.command('foo bar').action(() => {})

// Parent command (if any) must come after child commands.
app.command('foo').action(() => {})

aleclarson avatar Feb 10 '21 16:02 aleclarson

@egoist Would you merge a PR with https://github.com/aleclarson/cac/commit/227835ee2a2621626c2fa75393787258287aab95?

aleclarson avatar Jun 13 '22 21:06 aleclarson

@aleclarson, required args does not work properly. I guess required options too.

const app = cac('git');

app.command('remote add <name>', 'A git remote add.').action((name) => {
	console.log('remote add:', name);
});

app
	.command('remote rename <a> <b>', 'A git rename `a` to `b`.')
	.action((a, b) => {
		console.log('remote rename a to b', a, b);
	});

app
	.command('remote remove', 'Delete something')
	.alias(['rm', 'del', 'rmove', 'remoev'])
	.action(() => {
		console.log('remote remove');
	});


app.parse();

Because when checking for required it checks against this.cli.args, which when you run remote add is ['add'] and this.args length is 1 too, so it does not throw.

In the isMatched if add

	// if no sub-commands, it will be length 1, as currently
	const sub = command.name.split(' ')
    const parsedInfo = __assign(__assign({}, parsed), {
      args: parsed.args.slice(sub.length)
    });

The magic of Open Source :heart_eyes:

tunnckoCore avatar Jul 13 '22 17:07 tunnckoCore

~~Well.. there's another issue now.. :D Aliases doesn't work correctly.~~

~~They work. I just used to have an array there .alias(['rm', 'rmove', 'remoev'], which is another feature.~~

No they don't, but I know why that is.

Update isMatched to include this

this.path.every((part, i) => name[i] === part || this.aliasNames.includes(name[i]))

example:

[sigma@arckos preset-ens]$ node src/cac.js --help
cli/0.1.0

Usage:
  $ cli <command> [options]

Commands:
  ens create <collection>                      Create collection
  ens verify <collection>                      Make a collection verified
  ens certify <collection>                     Make a collection certified
  ens add names [...names]                     Add missing names to a collection
  ens add community [...socials]               Add community links to a collection.
  ens add website [...websites]                Add website links to a collection.
  remote add <name>                            A git remote add.
  remote rename <a> <b>                        A git rename `a` to `b`.
  remote remove                                Delete something
  remote set-branches [--add] <name> <branch>  Set branch.

For more info, run any command with the `--help` flag:
  $ cli ens create --help
  $ cli ens verify --help
  $ cli ens certify --help
  $ cli ens add names --help
  $ cli ens add community --help
  $ cli ens add website --help
  $ cli remote add --help
  $ cli remote rename --help
  $ cli remote remove --help
  $ cli remote set-branches --help

Options:
  -h, --help     Display this message 
  -v, --version  Display version number

Btw, would be good to limit the list of For more info, run any command to show just 3-5 commands. It says any command anyway, it should not list "all".

tunnckoCore avatar Jul 13 '22 17:07 tunnckoCore

@egoist want a PR? I'm sending it soon.

tunnckoCore avatar Jul 13 '22 18:07 tunnckoCore