argparse

A forum for general discussion of the Python programming language.

argparse

Postby pcoder » Mon Sep 26, 2016 2:21 pm

I have been using argparse. What I want to do is something like this.


scriptname.py -v


Code: Select all
parser = argparse.ArgumentParser(description='Going to add stuff for now just -v')
parser.add_argument('-v','--verbose',required=False,default="N")
args = parser.parse_args()


So if I don't pass a parameter it works fine as my understanding is it is using the value in default. If i do the -v I have to put some text after that. I guess what I want is switch functionality more the parameter functionality if that makes sense. Any suggestions?
pcoder
 
Posts: 25
Joined: Wed Jun 04, 2014 4:21 pm

Re: argparse

Postby metulburr » Mon Sep 26, 2016 3:09 pm

How many arguments are required?

If you just want 1
you can do
Code: Select all
parser.add_argument('-S', '--straight', action='store', type=str,
   help='go straight to the named game')

This would require the command
Code: Select all
filename.py -S stringer

type=str defines that it must be a string
IF you do not give it a value for the argument you will return an error such as
Code: Select all
usage: filename.py [-h] [-S STRAIGHT]
filename.py: error: argument -S/--straight: expected one argument


But if you want more than one arg such as window size
Code: Select all
parser.add_argument('-s' , '--size', nargs=2, default=[800,600], metavar=('WIDTH', 'HEIGHT'),
   help='set window size to WIDTH HEIGHT, defualt is {}'.format((800,600)))

This requires 2 arguments such as
Code: Select all
filename.py -s 1920 1080

default is optional for if the arg is not given at all, metavar is the description in help and is also optional, nargs tells how many args are required
IF you dont put the correct values in for your arg that you specified in nargs, you will also get an error and output of
Code: Select all
usage: filename.py [-h] [-s WIDTH HEIGHT]
filename.py: error: argument -s/--size: expected 2 argument(s)

I just realized that using s/S was prolly not a good idea, but this does illustrate the fact that it is cap sensitive.
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: argparse

Postby pcoder » Mon Sep 26, 2016 5:48 pm

I see what your saying. So if I require a argument then i have to put some value after the argument. Is there a way to just use -v with no text?


Suppose my script will do three things. 1 would be print and I would assign that a -p. 2 would be insert a record and I would assign that -i. 3 would be debug and I would assign that a -v.

I want to run it like this
scriptname.py -p
scriptname.py -i
scriptname.py -v


Is that not possible? I don't have to use argprese I just have used it in the past so that where I started.
pcoder
 
Posts: 25
Joined: Wed Jun 04, 2014 4:21 pm

Re: argparse

Postby metulburr » Mon Sep 26, 2016 7:23 pm

oh then you jsut add each argument such as
Code: Select all
import argparse

parser = argparse.ArgumentParser(description='My Arguments')
parser.add_argument('-p' , '--print', action='store_true',
   help='print this')
parser.add_argument('-i' , '--insert', action='store_true',
   help='insert this')
parser.add_argument('-d' , '--debug', action='store_true',
   help='insert this')
args = vars(parser.parse_args())
print(args)

Where action store true is defaulted to false, but returns true upon giving that argument

and example output
Code: Select all
metulburr@ubuntu:~$ python test2.py
{'print': False, 'insert': False, 'debug': False}
metulburr@ubuntu:~$ python test2.py -d
{'print': False, 'insert': False, 'debug': True}
metulburr@ubuntu:~$ python test2.py -pd
{'print': True, 'insert': False, 'debug': True}
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: argparse

Postby pcoder » Thu Sep 29, 2016 8:23 pm

That worked just like I wanted. Thanks.
pcoder
 
Posts: 25
Joined: Wed Jun 04, 2014 4:21 pm

Re: argparse

Postby metulburr » Fri Sep 30, 2016 1:31 am

no problem. IF you have any more questions feel free to ask. MAke sure to make a new accoutn on our new forums as we are locking this one down oct 1st
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 4 guests