Override the positional and optional arguments with another argument in command line (argparse python module) -
i using argparser parse command line arguments.
now, have
./script.py 1112323 0 --salary 100000 -- age 34
here first 2 positional arguments , rest optional.
now, want have feature such when user gives filename input in command line, should override these above arguments , take arguments header of file. meam when user gives sth
id|sequence|age|name|........... (header of file first 2 cols positional arguments , rest positional)
on giving in command line:
./script.py -f filename
it should not complain of above positional arguments.
is feasible on current implementation?
you need implement check yourself. make both arguments (positional , -f) optional (required=false , nargs="*") , implement custom check , use error method of argumentparser. make easier user mention correct usage in string.
something this:
parser = argumentparser() parser.add_argument("positional", nargs="*", help="if don't provide positional arguments need use -f") parser.add_argument("-f", "--file", required=false, help="...") args = parser.parse_args() if not args.file , not args.positional: parser.error('you must use either -f or positional argument')
Comments
Post a Comment