Skip to main content
Minor formatting improvements.
Source Link

Using the same option multiple times in Python's Argparseargparse

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py \ -i input1_url input1_name input1_other_var \  -i input2_url input2_name input2_other_var \ -i input3_url input3_name   # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, itargparse. It seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*'nargs='*' or nargs='+'nargs='+'), but that still won't let me use the -i-i flag multiple times. How do I go about accomplishing this.?

Just to be clear, what I would like in the end is a list of listlists of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 

Using the same option multiple times in Python's Argparse

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py -i input1_url input1_name input1_other_var -i input2_url input2_name input2_other_var -i input3_url input3_name # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, it seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this.

Just to be clear, what I would like in the end is a list of list of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 

Using the same option multiple times in Python's argparse

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py \ -i input1_url input1_name input1_other_var \  -i input2_url input2_name input2_other_var \ -i input3_url input3_name  # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse. It seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this?

Just to be clear, what I would like in the end is a list of lists of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 
Rollback to Revision 1
Source Link
jonrsharpe
  • 123.3k
  • 31
  • 277
  • 488

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py -i input1_url input1_name input1_other_var -i input2_url input2_name input2_other_var -i input3_url input3_name # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, it seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this.

Just to be clear, what I would like in the end is a list of list of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 

Edit --

I tweaked the answer by the nice fella below and got it working

parser.add_argument("-i", "--input", nargs='+', action='append', help=idesc) 

This works just fine :

python segment_videos.py -i the_url1 the_name1 arg1 -i the_url2 the_name2 Namespace(input=[['the_url1', 'the_name1', 'arg1'], ['the_url2', 'the_name2']]) 

The only issue is that the arguments to the -i flag aren't documented well in the help section. I'm now thinking it might be better to flush out each separate sub-argument to it's own flags.

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py -i input1_url input1_name input1_other_var -i input2_url input2_name input2_other_var -i input3_url input3_name # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, it seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this.

Just to be clear, what I would like in the end is a list of list of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 

Edit --

I tweaked the answer by the nice fella below and got it working

parser.add_argument("-i", "--input", nargs='+', action='append', help=idesc) 

This works just fine :

python segment_videos.py -i the_url1 the_name1 arg1 -i the_url2 the_name2 Namespace(input=[['the_url1', 'the_name1', 'arg1'], ['the_url2', 'the_name2']]) 

The only issue is that the arguments to the -i flag aren't documented well in the help section. I'm now thinking it might be better to flush out each separate sub-argument to it's own flags.

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py -i input1_url input1_name input1_other_var -i input2_url input2_name input2_other_var -i input3_url input3_name # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, it seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this.

Just to be clear, what I would like in the end is a list of list of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 
simple typo
Source Link
John Allard
  • 4k
  • 5
  • 28
  • 47

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py -i input1_url input1_name input1_other_var -i input2_url input2_name input2_other_var -i input3_url input3_name # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, it seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this.

Just to be clear, what I would like in the end is a list of list of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 

Edit --

I tweaked the answer by the nice fella below and got it working

parser.add_argument("-i", "--input", nargs='+', action='append', help=idesc) 

This works just fine :

python segment_videos.py -i the_url1 the_name1 arg1 -i the_url2 the_name2 Namespace(input=[['the_url1', 'the_name1', 'arg1'], ['the_url2', 'the_name2']]) 

The only issue is that the arguments to the -i flag aren't documented well in the help section. I'm notnow thinking it might be better to flush out each separate sub-argument to it's own flags.

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py -i input1_url input1_name input1_other_var -i input2_url input2_name input2_other_var -i input3_url input3_name # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, it seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this.

Just to be clear, what I would like in the end is a list of list of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 

Edit --

I tweaked the answer by the nice fella below and got it working

parser.add_argument("-i", "--input", nargs='+', action='append', help=idesc) 

This works just fine :

python segment_videos.py -i the_url1 the_name1 arg1 -i the_url2 the_name2 Namespace(input=[['the_url1', 'the_name1', 'arg1'], ['the_url2', 'the_name2']]) 

The only issue is that the arguments to the -i flag aren't documented well in the help section. I'm not thinking it might be better to flush out each separate sub-argument to it's own flags.

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this

./my_script.py -i input1_url input1_name input1_other_var -i input2_url input2_name input2_other_var -i input3_url input3_name # notice inputX_other_var is optional 

But I can't quite figure out how to do this using argparse, it seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option (nargs='*' or nargs='+'), but that still won't let me use the -i flag multiple times. How do I go about accomplishing this.

Just to be clear, what I would like in the end is a list of list of strings. So

[["input1_url", "input1_name", "input1_other"], ["input2_url", "input2_name", "input2_other"], ["input3_url", "input3_name"]] 

Edit --

I tweaked the answer by the nice fella below and got it working

parser.add_argument("-i", "--input", nargs='+', action='append', help=idesc) 

This works just fine :

python segment_videos.py -i the_url1 the_name1 arg1 -i the_url2 the_name2 Namespace(input=[['the_url1', 'the_name1', 'arg1'], ['the_url2', 'the_name2']]) 

The only issue is that the arguments to the -i flag aren't documented well in the help section. I'm now thinking it might be better to flush out each separate sub-argument to it's own flags.

I figured it out and was updating to reflect as such
Source Link
John Allard
  • 4k
  • 5
  • 28
  • 47
Loading
Source Link
John Allard
  • 4k
  • 5
  • 28
  • 47
Loading