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

Default command would be helpful #2

Open
justinmchase opened this issue Apr 9, 2022 · 7 comments
Open

Default command would be helpful #2

justinmchase opened this issue Apr 9, 2022 · 7 comments

Comments

@justinmchase
Copy link

justinmchase commented Apr 9, 2022

I could see it being solved a couple of ways but below is one way to illustrate the suggestion. Basically add an options object to the .subcommand(name, command, opts) function which has { default: boolean }.

If more than one subcommand is set as default then an error is thrown.

When parsing the command if no subcommand is matched then the default command is selected and parsing continues with that command.

import {
  choice,
  Command,
  CommandGroup,
  integer,
} from "https://deno.land/x/clay/mod.ts";

const create = new Command("Create a new resource.");
const destroy = new Command("Destroy a given resource.")
  .required(integer, "id")
  .required(choice("CONFIRM", ["y", "yes", "ok"]), "confirm", {
    flags: ["confirm"],
    description: "Confirm this action.",
  });

const manage = new CommandGroup("Manage imaginary resources.")
  .subcommand("create", create, { default: true })
  .subcommand("destroy", destroy);

console.log(manage.run()); // { create?: {}, destroy?: { id: number, confirm: "y"|"yes"|"ok" } }

Example Usage

example               # { create: {} }
example create        # { create: {} }
example destroy 123 y # { destroy: { id: 123, confirm: y } }
@dyedgreen
Copy link
Owner

What do you mean with “default command”?

@justinmchase
Copy link
Author

@dyedgreen Description updated. Sorry, got distracted when creating this.

@justinmchase
Copy link
Author

Thoughts on this?

@dyedgreen
Copy link
Owner

Yep, I think having something like this is a good idea!

Would you be open to contributing this as a PR?

@justinmchase
Copy link
Author

Ok, if you're not opposed to the feature I will try.

@denizdogan
Copy link

Did anything come out of this idea?

@justinmchase
Copy link
Author

No sorry I didn't have time and forgot to followup.

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

3 participants