cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Ability to define arguments as well (like flags)

Open andygrunwald opened this issue 9 years ago • 11 comments

Thanks for cobra. It is a really incredible project. Kudos.

In the PHP language there is a similar library quite popular: symfony/console. In this library you are able to define flags and arguments and access them via names. This makes development quite readable and convenient.

In cobra arguments are treated as a slice / list instead of defined names. See cobra/cmd/add.go#L47 as an example.

In symfony/console you would write something like

protected function configure()
{
    $this
       // ...
        ->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?')
        ->addArgument('last_name', InputArgument::OPTIONAL, 'Your last name?')
    ;
}

and access it like

protected function execute(InputInterface $input, OutputInterface $output)
{
    $text = 'Hi '.$input->getArgument('name');

    $lastName = $input->getArgument('last_name');
    if ($lastName) {
        $text .= ' '.$lastName;
    }

    $output->writeln($text.'!');
}

A matching call would look like php appname Andy Grunwald.

Did you considered such a behaviour for cobra as well? What is your opinion about it?

I don`t know much about cobras internal codebase (yet), but i assume this wouldnt be a BC. I like to hear your opinion and feedback about it.

A few documentation references that might be useful for this:

andygrunwald avatar Jan 06 '17 22:01 andygrunwald

Coming from the world of PowerShell, I would like to see an option to define a position for flags that would bind positional arguments.

RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from", 0)

So the following commands would be valid.

myexe --source /src
myexe -s /src
myexe /src

All three bind to Source. Any arguments after that would go to []args.

I'm not sure if that's taboo in the POSIX world, but it makes for a convenient command-line user experience.

cdhunt avatar May 25 '17 13:05 cdhunt

Some progress was made in #284

n10v avatar Jul 24 '17 16:07 n10v

While relevant, I don't think #284 is the same as this. This asks for a way to properly specify and document arguments, and likely some type of automation of what #284 added.

Naatan avatar Jan 12 '18 19:01 Naatan

Agreed. I'd go one step further and say there should be the ability to validate types of positional arguments as well. I can already specify that my command takes a --foo argument whose value is of type int, and cobra/pflags will validate the user's input, convert it to an int, and bind it to a corresponding variable. But if I want it as a positional argument I have to roll my own everything.

ceejatec avatar Aug 10 '18 23:08 ceejatec

Python's built-in argparse library handles this very smoothly. There's no difference at all between a positional and flag-based argument except that one is named eg. "foo" and the other is named "--foo". Everything else - typing, validation, number of values, etc - is exactly the same. And the auto-generated help output documents them all correctly.

ceejatec avatar Aug 10 '18 23:08 ceejatec

This issue is being marked as stale due to a long period of inactivity

github-actions[bot] avatar Apr 13 '20 00:04 github-actions[bot]

++ Following argparse's lead on positional arguments.

alecb-stripe avatar Feb 26 '21 16:02 alecb-stripe

Just started using this package today I was having so much fun until I ran into this :(

mlaforet avatar Sep 28 '21 21:09 mlaforet

Consistent activity even over a few years with some thoughtful discussion. I think this warrants some extra thought but would definitely need a full proposal as it seems like a potentially large change and the implementation that can conserve backwards compatibility isn't clear.

johnSchnake avatar Mar 16 '22 06:03 johnSchnake

I was just trying to figure out how to bind arguments to variable names and came to this issue, so I wanted to add a +1 to show interest.

gwynforthewyn avatar Mar 23 '22 21:03 gwynforthewyn

Hello! I know this has been open for some time but I'd like to bump this a little. I find it truly baffling that there is no way to document positional arguments, it feels like something pretty foundational to CLI apps...

rmonvfer avatar Aug 13 '25 12:08 rmonvfer