Solved

Python: Using Argparser

Posted on 2010-08-20
4
1,241 Views
Last Modified: 2012-05-10
Using Argparser can I separate multiple command line arguments with a comma rather than a whitespace? Currently "my_script.py -i NY, Philly, Baltimore" gives ['NY, Philly, Baltimore'] instead of ['NY', 'Philly', 'Baltimore'].
0
Comment
Question by:abuhaneef
4 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 33493737
Can you post your code?  And explain more what you want to do?   Do you really want NY, Philly and Baltimore to be individual arguments or do you want them as options to your '-i' argument?  

Based on the command-line you posted (rather than what you asked), it seems like you'd want something like this (handles commas or spaces) and returns NY, Philly, Baltimore as a list of values for '-i':

import sys
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument("-i", nargs="*")
args = parser.parse_args()
args.i = re.split(r'[\s,]+'," ".join(args.i))
print args.i


---------------------------------------------------------------------


If you really are asking to get the comma to count as a command-line separator-- I don't think that's what you really want to do but it's what you said you wanted to do--- then this would probably work:

arg = parser.parse_args(re.split(r'[\s,]+'," ".join(sys.argv[1:])))
0
 
LVL 16

Expert Comment

by:gelonida
ID: 33495906
I'd do argument parsing in two steps.

let the argument parser parse the fields separately and get "NY,Phily,..." in one variable.

use then split to split further.

should NY,Phily,..." all belong to command option ;-i; or is I independent?

If you don't know whether the user passes the argument separated with spaces or with ',', then you had to write slightly more complicated cocde.


Ideally you show usyour existing code snippet,
 the command line,
the expected result

 (From your expected result it's not really clear what happened with tthe '-i' for example.
0
 
LVL 15

Accepted Solution

by:
mish33 earned 125 total points
ID: 33597788
You can easily convert  ['NY, Philly, Baltimore'] to ['NY', 'Philly', 'Baltimore'] by
iargs = iargs[0].replace(',',' ').split()

Open in new window

0
 
LVL 3

Expert Comment

by:T1750
ID: 33670763
mish33 is the correct answer beceause it's simple, quick to use and solves your proble, you should accept it.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question