Solved

Python: Using Argparser

Posted on 2010-08-20
4
1,257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 17

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

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Suggested Courses

630 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