Solved

Python: Using Argparser

Posted on 2010-08-20
4
1,249 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
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…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

752 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