2

The title may be confusing but I can't think of a better explanation. Basically, I have a program which operates on some input file with a bunch of optional arguments. The input file is compulsory for my program. So I wrote a parser like this:

test.py

from argparse import ArgumentParser

parser = ArgumentParser(prog="prog")
parser.add_argument("file", help="input file")
parser.add_argument("--debug", action="store_true", help="enable debug messages")
parser.parse_args()

I'm not showing all the options for simplicity. Now, I want to add a feature to this program which don't require input file and runs on its own. Let's call this option as a. Option a takes unknown number of arguments and --debug or other options are not valid for option a. So the following would be valid runs for my program:

$ python3 test.py input.txt
$ python3 test.py input.txt --debug
$ python3 test.py -a 1 2 3 

And these would be invalid:

$ python3 test.py input.txt -a 1 2 3 
$ python3 test.py -a 1 2 3 --debug

When I add this line to my code

parser.add_argument("-a", nargs="+", help="help option a")

It accepts option a but it says "file is required" for understandable reasons. So how should I modify my code to achieve my goal?

P.S: I also tried using subparser but I couldn't manage to make it work as I want probably because my lack of knowledge.

Asocia
  • 5,935
  • 2
  • 21
  • 46
  • 1
    You can use [Mutually Exclusive Groups](https://docs.python.org/3/library/argparse.html#mutual-exclusion) to achieve this. See this [stack overflow thread](https://stackoverflow.com/a/39459093/11778344) that solves a similar issue. – S V Praveen Jul 03 '20 at 18:19
  • @SVPraveen Yeah, it is very similar to my problem. However, it is raising an error saying `mutually exclusive arguments must be optional` when I try it. The argument `file` is not optional in my case. – Asocia Jul 03 '20 at 18:53
  • And [here](https://stackoverflow.com/q/29921862/9608759) is a related question. It seems that `argparse` doesn't support multiple argument groups as mutually exclusive. – Asocia Jul 03 '20 at 19:06
  • Assuming you don't want to just add `nargs="?"` for the `file` argument and make it optional? – thekid77777 Jul 03 '20 at 19:41
  • @thekid77777 Yes, `file` is required when `-a` option is not used. – Asocia Jul 03 '20 at 19:46
  • 1
    Ok no problem. I usually set my CLI tools up so they can handle a None type (handles like an incorrect argument) which results from not passing the mandatory argument. Easy way around that. – thekid77777 Jul 03 '20 at 19:49
  • @thekid77777 Yes, that might solve the `file` being required case. But another problem arises as I mentioned in my second comment. I want to be able to use `--debug` option with `file` but not with `-a`. So there are 2 groups of commands which shouldn't be mixed with each other. Adding mutually exclusive groups works like either `file` or `-a` is required. And it doesn't let me add `--debug` option to use with `file`. – Asocia Jul 03 '20 at 19:57
  • `mutually-exclusive` is the only built-in interaction tool (other than subparsers). It is relatively simple, with an optional required parameter, and accepting one optional positional. But even so it's almost too complex. Other testing is left up to the developer. Keep in mind, though, that you have to explain the constraints to your users (or your future self). – hpaulj Jul 03 '20 at 20:45

0 Answers0