3

I am trying to append a url by passing the parameters by commandline argument. Here is how I am trying:

import argparse
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument('input', metavar='input', type=str)
parser.add_argument('output', metavar='text', type=str)
args = parser.parse_args()
url = 'https://example.com/?z=12&text='+args.output+'&loc{}'
print url

When I execute

python url.py text.csv hello

It appends the second passed argument to the url. I want to know how to make the second argument optional so that even without providing the second argument I want the url to be printed by concatenating nothing to the url. Here is the output I am expecting:

When both arguments given:

python url.py text.csv hello

The output should be

https://example.com/?z=12&text=hello&loc{}

When single argument given

python url.py text.csv

The output should be

https://example.com/?z=12&text=&loc{}
2
  • Other than setting a default of ''? Commented Jun 16, 2016 at 11:18
  • Setting default? Can you please explain @IgnacioVazquez-Abrams. I am still learning. Commented Jun 16, 2016 at 11:20

2 Answers 2

6

Try nargs='?' and define a default:

import argparse
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument('input', metavar='input')
parser.add_argument('output', metavar='text', nargs='?', default='')
args = parser.parse_args()
url = 'https://example.com/?z=12&text='+args.output+'&loc{}'
print url

When I test it with just one commandline string, args.output is the default:

In [91]: args = parser.parse_args(['one'])

In [92]: args
Out[92]: Namespace(input='one', output='')

In [93]: args = parser.parse_args(['one','two'])

In [94]: args
Out[94]: Namespace(input='one', output='two')

It is best to only use nargs='?' (or '*' or '+') with the last positional argument. It's possible to use it on earlier ones, but there are complications that can send you back with more questions.

Sign up to request clarification or add additional context in comments.

Comments

1

As I see it, you have two options. Either you avoid using argparsealtogether and go for something like:

import sys
args_input  = sys.argv[1]
args_output = sys.argv[2] if len(sys.argv) > 2 else ''
url = 'https://example.com/?z=12&text='+args_output+'&loc{}'
print url

Or you add the - to your optional argument and, as Ignacio's reply suggests, you set a default empty value for the output argument:

import argparse
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument('input', metavar='input', type=str)
parser.add_argument('-output', metavar='text', type=str, default='')
args = parser.parse_args()
url = 'https://example.com/?z=12&text='+args.output+'&loc{}'
print url

With this second option you'll have to call it like:

python url.py text.csb -output hello

Or

python url.py text.csb

but it is more extensible if you want to add more arguments afterwards.

3 Comments

If first argument is always passed, it might be a cleaner syntax to only put a - in front of output. Then the program can be called with python url.py text.csb -output hello or python url.py text.csb
I completely agree! I was just thinking about that!!! I'll re-edit to show your suggestion! Thanks!
yea. thank you. I opted to add - to the second argument parser. I thought that would be easy addition than changing the rest of the code to sys.argsv[]. I did just like you said to another script using sys.argsv[] @jaumebonet . Thanks again to everyone. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.