question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to pass options before a sub-command ?

See original GitHub issue

Hello,

Thanks a lot for the cliffy work, I just started using it but I already love the method chaining approach;

However I’d like to build a CLI in a way that doesn’t seem to be allowed by the framework

abcdctl -a localhost -p 8080 chain --from source --to destination --amount 69

My basic command abcd has several sub-commands (in this example chain), but the server arguments (-a for the addr & -pfor the port) are global to all sub-commands

Here is my try

let remote: Client;

await new Command()
  .name("abctl")
  .version("1.0.0")
  .description("Interact with abcd")
  .option("-a, --address <addr>", "HTTP endpoint", { default: "localhost" })
  .option("-p, --port <port>", "HTTP port", { default: 8080 })
  .action(async ({ address, port }: { address: string; port: number }) => {
    remote = await new Client(address, port).co.catch(_ => Deno.exit(1));
    await remote?.ping().then(console.dir);
  })
  .command("chain", new Command()
    .description("Interact with blockchain")
    .version("0.1.0")
    .option("-f, --from <from:string>", "Source wallet")
    .option("-t, --to <to:string>", "Destination wallet")
    .option("-a, --amount <amount:number>", "Amount")
    .action(async ({ from, to, amount } : {
      from: string,
      to: string,
      amount: number
    }) => {
      const response = await remote.chainadd(from, to, amount);
      console.dir(response);
      Deno.exit(0)
    })
  )
  .parse(Deno.args);

The problem is that when I execute the example I get an error stating that the “chain” command does not exist (and it offers me to try “chain” instead)

Is my objective reachable with this framework or I must change my approach ?

Thanks a lot

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
soootalebcommented, Sep 12, 2021

Hello @c4spar the arguments before the sub command are common and required by all sub commands, that’s why I’d rather see them before the sub command even if it is not a hard requirement

1reaction
c4sparcommented, Jul 29, 2022

It’s now supported in v0.24.3 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add common options to sub commands which can go ...
Click strictly separates parameters between commands and subcommands. What this means is that options and arguments for a specific command ...
Read more >
How do I configure global options for all subcommands? #895
The obvious use case here is logging. This issue isn't about how to specify global options (e.g. an option that works for all...
Read more >
Clikt - Common Options With Subcommands
You can define your options on the root command and pass down the information via the context. With this design, you'll have to...
Read more >
Adding arguments and options to your Bash scripts - Red Hat
The ability to use positional parameters—otherwise known as arguments—to specify data to be used as values for variables in the scripts is one ......
Read more >
Picocli subcommands - aragost.com
The subcommand is specifed as a parameter to the main application. Arguments passed before the subcommand name are considered to belong to the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found