Your idea doesn't make sense from the user side of things.
Meaning: when you say -f
the user already has to understand that the tool will open the corresponding file and process it. Therefore you shouldn't ask him to provide -a
on top of that.
But of course you can do it. What I would do here: provide a default value for the -a
switch, as outlined here for example.
Then your script can check if that default value is used. And then you can make decisions based on that, like in:
- if
-a
has its default value and -f
is given: read from file and do what -a
should do with it
- if
-a
has not its default value and -f
is given: ??? (you have to think up what that means)
- ...
Those ... are a symptom of why I consider your approach to not be elegant: it really complicates the whole argument handling massively. I would just go for -a NUMBER
or -f file
and not allow for those to be mixed. You see, any interface should make it easy to do the right thing, and hard to do the wrong thing. Allowing for combinations of flags/switches simply leads to an explosion of possible paths. And that means that you have to deal with all of them, and the user has to remember those that are actually valid.
And given your comment: of course you could unify this to have -a
take either a number or a string denoting a filename.
Then you simply instruct argparse that -a
stores string values. Then your script can decide whether that string is a number or a valid filename. To then do what is necessary. Something like this:
from __future__ import print_function
import os.path
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--add",
help="whatever",
type=str)
args = parser.parse_args()
if args.add:
if args.add.isdigit():
print("input contains only digits: {}".format(args.add))
else:
print("input contains other stuff: {}".format(args.add))
if os.path.isfile(args.add):
print("input is a valid file name!")