1

I'm using a click option like this in my code :

@click.option('--stepids', '-s', required=True, is_flag=True)

But when I launch my command whithout the -s option, it executes without raising any error. I guess the flag got an implicit value as False, how can I set it to nothing so my script will tell "error missing -s argument" ? I tried default=' ' but it didnt work.

In case you find it bizarre, I'm using this trick to simulate a multi-value option with an unknown number of arguments.

@click.option('--stepids', '-s', required=True, is_flag=True)
@click.argument('stepsids', nargs=-1)

I can then use it like this "my script -s 10 11 2..."

Thanks ^^

  • Specifically [this](https://stackoverflow.com/a/54764354/) answer. You wouldn't want to use `is_flag`, instead use `nargs=0`. – metatoaster Mar 22 '21 at 11:02
  • Thanks but upon trying, it dosnt work (maybe got patched ?). It keeps telling "error missing option -s". The support gave me the solution of packing the numbers in a string between quotes.... but I dont like it either. At least, if there could be a default empty value for the bool flag... but there is none, making the value impossible to be required. – BabaDeathLord Mar 23 '21 at 08:46
  • Have you also take a look at the [accepted answer](https://stackoverflow.com/a/48394004/)? I pointed out the shortcut solution as that matches closely with what you were trying to do, so if does not satisfy what you actually want, you will need to integrate the accepted answer with your code. Otherwise, please update your question to include a test case (similar to the accepted answer) with the expected behavior for the presence/absence/error argument cases, though I think if your program will only accept a single `stepids` argument, it's best to not require an extra flag as that's redundant. – metatoaster Mar 23 '21 at 12:17
  • In any case, please also read the question above the linked answers as one of the updates included a link to the [github issue](https://github.com/pallets/click/issues/484) that rejected the proposal to include multi-value option accompanied with a flag (the thing you are trying to simulate in your question). – metatoaster Mar 23 '21 at 12:23
  • Ooooh sorry I didnt see it, im new here x) Thank you this is what I was looking for ^^ – BabaDeathLord Mar 23 '21 at 13:29

0 Answers0