-3

I am new to argparser in python . I am trying to create argparser for file which contains two functions download and upload file on/from box. It will do only one functionality at once according according to that i am trying to create parser for that file as follows but it's not working for me:

parser = argparse.ArgumentParser(description='Download or Upload file on box.')
    parser.add_argument('-df', '--download', required=True,
                        help='download file box')
    parser.add_argument('-uf', '--upload', nargs='+', required=True,
                        help='upload file of box')
    parser.add_argument('-fp', '--filepath', required=True,
                        help='file path to upload(which file to upload) or download(where to download file)')
    parser.add_argument('-fn', '--filename', required=True,
                        help='what should be the name of file on box')
    parser.add_argument('-fi', '--fileid', required=True,
                        help='file id of file to download from box')

    args = vars(parser.parse_args())

NOTE :- every time only -df or -uf options will be there, -fp is mandatory for both and if it is -df then -fi is only option and it is mandatory and if -uf then -fn is only option and it's mandatory.

How do I achieve this, following are example how will i pass argument to file

pyhton abc.py -df -fp 'Download/boxfile/xyz.txt' -fi 123

python abc.py -uf -fp 'Download/boxfile/xyz.txt' -fn 'qwe.txt'

khelwood
  • 55,782
  • 14
  • 81
  • 108
Piyush S. Wanare
  • 4,703
  • 6
  • 37
  • 54
  • Could you explain your NOTE paragraph more clearly? It is difficult to follow – khelwood Jun 23 '18 at 06:54
  • rather than down voting question you can do hard work to comment and ask more question . as i dint think `argsparser` document is more understandable for fresher. – Piyush S. Wanare Jun 23 '18 at 07:06
  • @khelwood, Thanks bro !! as I want to two options `-df`(download file) and `-uf`(upload file) only one at time. `-fp`(file path) will be mandatory for both `-df` and `-uf`. The options `-fn`(file name) and `-fi`(file id) will be depends on `-df` and `-uf`, if `-df` then only `-fi` is the option and it's mandatory and If `-uf` then only `-fn` is the option ans it's mandatory. – Piyush S. Wanare Jun 23 '18 at 07:10

3 Answers3

1

I'm not incredibly familiar with argparse, however from reading the documentation I don't believe there's a way to use mutually_exclusive_group and required to force this. It seems conditional statements or equivalent are necessary to confirm that valid parameter combinations were passed.

See: Python Argparse conditionally required arguments

hamurai
  • 11
  • 1
1

I'd suggest that you get rid of most of these arguments, maybe all, and think about standard Unix ways of doing things.

Consider scp, which has a syntax of: scp source destination

For instance: scp Download/boxfile/xyz.txt qwe.txt

If you don't supply a destination, it infers that you want the file to be called the same thing, and land right here, so these two things are equivalent:

scp Download/boxfile/xyz.txt xyz.txt
scp Download/boxfile/xyz.txt

Of course, scp can talk to machines across the internet, so there is a format for that:

scp hostname:Download/boxfile/xyz.txt xyz.txt

And if you are uploading, you simple switch the order:

scp xyz.txt hostname:Download/boxfile/xyz.txt
Hack Saw
  • 2,741
  • 1
  • 18
  • 33
1

As written all 5 of the arguments are required - you made that explicit. If that's what you really want, all the rest of the question is irrelevant. You'll have to provide all 5 regardless.

But the comments indicate that you want to use either -df or -uf, but probably not both (though that bit's unclear).

While there is a mutually_exclusive_mechanism in argparse, there isn't a inclusive equivalent - something that says if -f is present, then -g must also be given.

But subparsers mechanism can be used that way. You could define an download subparser, with a required -fi argument. And an upload with its own argument.

Another option is to set -df to take 2 arguments (nargs=2), both its box and its file.

If -df and -uf are mutually exclusive, why not use the same argument for the name of the file? That is, replace -fn and -fi with one file argument.

Another option is to make all (or most) of the arguments not-required, and check for the correct combinations after parsing. It's easier to implement complicated logic in your own code than to force argparse to do it for you.

e.g.

 if args.download is not None:    # not default
     <check for `args.filename`> etc

It can also be a good idea to provide defaults for optional arguments. That way the code can run even if the user doesn't provide all items.

On a matter of style. Short option flags, with on - are usually a single character. -d, -u, etc. The ability to combine several into one string only works in that case, e.g. -df foo. In this case it probably doesn't matter since none of your arguments are store_true.

hpaulj
  • 221,503
  • 14
  • 230
  • 353