How to create Argument Parser

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'

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.

Argparse Tutorial, Note also that argparse is based on optparse , and therefore very similar in terms ArgumentParser() parser.add_argument("echo") args = parser.parse_args()  Parser for command-line options, arguments and subcommands The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv.

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

argparse – Command line option and argument parsing., Which module is used for parsing command line arguments automatically? You will now take each of the above options one by one and see how to use them for parsing arguments (and options). Let's go chronologically. Argument Parsing using sys.argv. Let's start simple. In this first example, you will create a Python file named demo1.py. Your program will accept an arbitrary number of arguments passed from the command-line (or terminal) while getting executed.

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

Argument Parsing in Python, etc. At the very simplest form, we can use it like below. parser.add_argument('-o', '--output', action='store_true', help="shows output") An argument is added with add_argument(). The action set to store_true will store the argument as True, if present. The help option gives argument help. args = parser.parse_args() The arguments are parsed with parse_args(). The parsed arguments are present as object attributes.

Python Command Line Arguments, How do you parse a command line argument in Python? In addition, once your parsing rules have been defined, TokenIcer can create a parser class, based on your rules, in either C# or VB.NET Background To be able to use TokenIcer well, you should have a pretty good understanding of how Regular Expressions work.

How to Build Command Line Interfaces in Python With argparse , parse_args() to parse the input arguments and get a Namespace object that contains the user input. Here are the steps we need to take in order to execute our simple_example.py script: Step 1: Download the zip accompanying this blog post from the "Downloads" section Step 2: Open a terminal and change directory to where the zip lives. Step 3: Unzip. Step 4: Change directory again, this time

Learn Enough Python to be Useful: argparse, Argument Parsing using sys.argv. Let's start simple. In this first example, you will create a Python file named demo1.py . Your program will  To my knowledge, the three most popular ways how to parse command line arguments in C are: Getopt (#include <unistd.h> from the POSIX C Library), which can solve simple argument parsing tasks. If you're a bit familiar with bash, the getopt built-in of bash is based on Getopt from the GNU libc.

Comments
  • Could you explain your NOTE paragraph more clearly? It is difficult to follow
  • 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.
  • @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.
  • Love that first sentence!