Timeline for How can I pass a list as a command-line argument with argparse?
Current License: CC BY-SA 4.0
33 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 26, 2024 at 1:35 | history | edited | Mateen Ulhaq | CC BY-SA 4.0 | Shrink headers. |
| Mar 8, 2024 at 9:22 | comment | added | djvg | Also see docs for nargs and specifying-ambiguous-arguments | |
| Feb 8, 2024 at 21:02 | comment | added | SethMMorton | @Avio I'm guessing the heuristic is getting tripped up by the scientific notation, so it doesn't recognize it as a float. | |
| Feb 8, 2024 at 13:40 | comment | added | Avio | Thanks @SethMMorton. It's me or the nargs method doesn't like negative floats in scientific notation? E.g. 5.73e-05 is perfectly fine but -5.73e-05 gives unrecognized arguments. This is the line, for the posterity: parser.add_argument('--list-nargs-float', nargs='+', type=float, help='a float-typed list param made with nargs') | |
| Dec 9, 2022 at 20:07 | comment | added | SethMMorton | @t7e If it's not working, I would anticipate that means it is not supported. | |
| Dec 9, 2022 at 16:49 | comment | added | t7e | can I somehow combine nargs with action='append' ? parser.add_argument('-n', '--namespace', nargs='+', action="append") This one does not work. | |
| Oct 6, 2021 at 2:00 | history | edited | SethMMorton | CC BY-SA 4.0 | deleted 4 characters in body |
| Oct 6, 2021 at 1:51 | history | edited | SethMMorton | CC BY-SA 4.0 | added 24 characters in body |
| Mar 16, 2020 at 16:51 | comment | added | SethMMorton | That’s too open-ended without more details and probably worth asking as a separate question. | |
| Mar 16, 2020 at 16:49 | comment | added | alper | What if i want to pass multiple list? how can I seperate them? @SethMMorton | |
| Nov 17, 2018 at 5:57 | comment | added | SethMMorton | @MataFu You mean like parser.add_argument('--nargs-int-type', nargs='+', type=int, default=[5]), or parser.add_argument('--nargs-int-type', nargs='+', type=int, default=[1, 2, 3, 4, 5])? | |
| Nov 16, 2018 at 14:18 | comment | added | Mata Fu | Thank you for your experiment. I wonder how to set the default value to method like parser.add_argument('--nargs-int-type', nargs='+', type=int) | |
| Aug 16, 2017 at 18:09 | comment | added | 0andriy | it can be unintuitive if there are positional arguments because argparse can't tell what should be a positional argument and what belongs to the nargs. -- helps to figure this out as shown in example in my previous comment. IOW user supplies -- followed by all positional arguments. | |
| Aug 16, 2017 at 17:00 | comment | added | SethMMorton | @0andriy Can you complete your thought? Right now this statement appears to be unrelated to the question or this answer, but I have a feeling that you had a good reason for mentioning it. How could the presence of -- improve this answer? | |
| Aug 15, 2017 at 15:51 | comment | added | 0andriy | -- could split options vs. positional arguments. prog --opt1 par1 ... -- posp1 posp2 ... | |
| Aug 3, 2017 at 15:28 | history | edited | SethMMorton | CC BY-SA 3.0 | added 26 characters in body |
| Jul 18, 2017 at 18:13 | history | edited | SethMMorton | CC BY-SA 3.0 | added 565 characters in body |
| Feb 8, 2017 at 15:44 | history | edited | SethMMorton | CC BY-SA 3.0 | added 174 characters in body |
| Oct 6, 2016 at 15:03 | history | edited | SethMMorton | CC BY-SA 3.0 | added 14 characters in body |
| Sep 6, 2016 at 14:37 | history | edited | SethMMorton | CC BY-SA 3.0 | added 282 characters in body |
| Jul 29, 2016 at 2:39 | history | edited | SethMMorton | CC BY-SA 3.0 | added 317 characters in body |
| Jul 11, 2016 at 3:46 | history | edited | SethMMorton | CC BY-SA 3.0 | added 8 characters in body |
| Jul 10, 2016 at 2:02 | history | edited | SethMMorton | CC BY-SA 3.0 | Gave a more thorough input example, and gave actual invocation examples and results. |
| Oct 15, 2015 at 13:30 | history | edited | SethMMorton | CC BY-SA 3.0 | deleted 2 characters in body |
| Sep 1, 2015 at 15:58 | comment | added | SethMMorton | @user1346466 One does not need the quotes for negative numbers, a space separated list works just as well for negative numbers as not. argparse seems to thing that numeric options would be illegal anyway and parses the - as part of the number, not as an argument (the merits of this assumption could be discussed elsewhere...). | |
| Sep 1, 2015 at 13:34 | comment | added | user1346466 | Take care with negative values. This can be quite tedious with nargs. Compare -l 10 ' -20' ' -30' with the more natural --list='10 -20 -30'. | |
| Oct 31, 2014 at 1:56 | history | edited | SethMMorton | CC BY-SA 3.0 | added 554 characters in body |
| Oct 30, 2014 at 13:55 | comment | added | SethMMorton | @Dror All input is assumed to be strings unless you set the type parameter to some other object. By default this method returns a list of strings. | |
| Oct 30, 2014 at 10:41 | comment | added | Dror | @SethMMorton But what if I want to give a list of strings as the argument? | |
| Sep 3, 2013 at 20:34 | comment | added | SethMMorton | @rd108 I see, I bet that you are using the type=list option. Don't use that. That turns a string into a list, and hence the lists of lists. | |
| Sep 3, 2013 at 20:04 | comment | added | rd108 | What about a list of strings? This turns multiple string arguments ("wassup", "something", and "else")into a list of lists that looks like this: [['w', 'a', 's', 's', 'u', 'p'], ['s', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g'], ['e', 'l', 's', 'e']] | |
| Apr 4, 2013 at 0:38 | vote | accept | carte blanche | ||
| Apr 1, 2013 at 23:37 | history | answered | SethMMorton | CC BY-SA 3.0 |