Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 3 tasks
trusktr opened this issue Feb 27, 2020 · 7 comments
Open
1 of 3 tasks

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

trusktr opened this issue Feb 27, 2020 · 7 comments

Comments

@trusktr
Copy link

trusktr commented Feb 27, 2020

Issue Type

  • Bug Report
  • 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.

@egoist
Copy link
Collaborator

egoist commented Feb 27, 2020

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

@trusktr
Copy link
Author

trusktr commented Feb 27, 2020

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

@trusktr trusktr changed the title sub commands like 'git remote prune origin'? [feature] sub commands like 'git remote prune origin' Feb 27, 2020
@aleclarson
Copy link

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

aleclarson@227835e

const app = cac()

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

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

@aleclarson
Copy link

@egoist Would you merge a PR with aleclarson@227835e?

@tunnckoCore
Copy link

tunnckoCore commented Jul 13, 2022

@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 😍

@tunnckoCore
Copy link

tunnckoCore commented Jul 13, 2022

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
Copy link

tunnckoCore commented Jul 13, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants