Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

argparse: delimiter for argparse list arguments

287 views
Skip to first unread message

Sven R. Kunze

unread,
Aug 2, 2021, 3:52:33 PM8/2/21
to
Hi everyone,

maybe, I am missing something here but is it possible to specify a
delimiter for list arguments in argparse:

https://docs.python.org/3/library/argparse.html

Usually, '--' is used to separate two lists (cf. git).

Cheers,
Sven

Dan Stromberg

unread,
Aug 2, 2021, 7:50:26 PM8/2/21
to
Isn't -- usually used to signal the end of options?
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Michael Torrie

unread,
Aug 3, 2021, 10:46:01 AM8/3/21
to
On 8/2/21 1:43 PM, Sven R. Kunze wrote:
> Hi everyone,
>
> maybe, I am missing something here but is it possible to specify a
> delimiter for list arguments in argparse:
>
> https://docs.python.org/3/library/argparse.html
>
> Usually, '--' is used to separate two lists (cf. git).

I've not seen this syntax in git. Are you referring the the double and
triple dot notation git uses? Can you give me an example of how git
uses -- as a list separator?

Typically -- on a command line means that's the end of the any special
switches and anything else, even if it looks like a command-line switch,
should not be parsed, and passed straight through as a normal parameter.

Jon Ribbens

unread,
Aug 3, 2021, 11:49:10 AM8/3/21
to
On 2021-08-03, Michael Torrie <tor...@gmail.com> wrote:
> On 8/2/21 1:43 PM, Sven R. Kunze wrote:
>> maybe, I am missing something here but is it possible to specify a
>> delimiter for list arguments in argparse:
>>
>> https://docs.python.org/3/library/argparse.html
>>
>> Usually, '--' is used to separate two lists (cf. git).
>
> I've not seen this syntax in git. Are you referring the the double and
> triple dot notation git uses? Can you give me an example of how git
> uses -- as a list separator?

Loads of git commands do this. e.g. commit, diff, log, status, etc.
It's not completely unlike what you're describing above, which is
already supported automatically by argparse.

Roel Schroeven

unread,
Aug 3, 2021, 3:11:03 PM8/3/21
to
Jon Ribbens via Python-list schreef op 3/08/2021 om 17:48:
Commands like git commit do not use '--' to separate two lists, but as
Dan and Sven said to separate options from arguments, even if the
arguments start with '-' (like many other programs -- I think this is
standard in GNU). If you're 100% certain that none of the filenames
start with '-', you can leave out '--' without any change in behavior.
Especially when scripting and/or using wildcards it's best always to use
that '--' to avoid nasty surprises.

$ git commit -m "hello" -- hello.py

is exactly the same as

$ git commit -m "hello" hello.py

It's just that the former is safer to use, because it properly works
even in cases like

$ git commit -m "hello" -- -hello.py

whereas

$ git commit -m "hello" -hello.py

will likely not do what you expect.

As https://git-scm.com/docs/git-commit says:

> --
>   Do not interpret any more arguments as options.

--
"The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom."
-- Isaac Asimov

Jon Ribbens

unread,
Aug 3, 2021, 4:56:05 PM8/3/21
to
On 2021-08-03, Roel Schroeven <ro...@roelschroeven.net> wrote:
> Jon Ribbens via Python-list schreef op 3/08/2021 om 17:48:
>> On 2021-08-03, Michael Torrie <tor...@gmail.com> wrote:
>> > On 8/2/21 1:43 PM, Sven R. Kunze wrote:
>> >> maybe, I am missing something here but is it possible to specify a
>> >> delimiter for list arguments in argparse:
>> >>
>> >> https://docs.python.org/3/library/argparse.html
>> >>
>> >> Usually, '--' is used to separate two lists (cf. git).
>> >
>> > I've not seen this syntax in git. Are you referring the the double and
>> > triple dot notation git uses? Can you give me an example of how git
>> > uses -- as a list separator?
>>
>> Loads of git commands do this. e.g. commit, diff, log, status, etc.
>> It's not completely unlike what you're describing above, which is
>> already supported automatically by argparse.
>
> Commands like git commit do not use '--' to separate two lists, but as
> Dan and Sven said to separate options from arguments,

That is not quite correct. Check out 'git diff' for example - it takes
as position arguments zero, one, or two commit hashes followed by zero
or more pathnames. You can use '--' to separate these two lists,
because of course sometimes a pathname can be indistinguishable from
a commit hash.

Sven R. Kunze

unread,
Aug 3, 2021, 5:06:40 PM8/3/21
to
It could be but I've seen them used somewhere else.

I wouldn't bikeshed on this yet, as I haven't found a way to do this so
far. Let's imagine the following parser:

parser.add_argument('things',action='append')
parser.add_argument('stuff',action='append')

At least from my point of view, I don't any way to separate both lists
on this command call:


cool-script.py thing1 thing2 stuff1 stuff2


Do I miss something here?


Best
Sven

On 03.08.21 01:49, Dan Stromberg wrote:
>
> Isn't -- usually used to signal the end of options?
>
> On Mon, Aug 2, 2021 at 12:52 PM Sven R. Kunze <srk...@mail.de
> <mailto:srk...@mail.de>> wrote:
>
> Hi everyone,
>
> maybe, I am missing something here but is it possible to specify a
> delimiter for list arguments in argparse:
>
> https://docs.python.org/3/library/argparse.html
> <https://docs.python.org/3/library/argparse.html>
>
> Usually, '--' is used to separate two lists (cf. git).
>
> <https://mail.python.org/mailman/listinfo/python-list>
>

Chris Angelico

unread,
Aug 3, 2021, 5:21:24 PM8/3/21
to
On Wed, Aug 4, 2021 at 7:07 AM Sven R. Kunze <srk...@mail.de> wrote:
>
> It could be but I've seen them used somewhere else.
>
> I wouldn't bikeshed on this yet, as I haven't found a way to do this so
> far. Let's imagine the following parser:
>
> parser.add_argument('things',action='append')
> parser.add_argument('stuff',action='append')
>
> At least from my point of view, I don't any way to separate both lists
> on this command call:
>
>
> cool-script.py thing1 thing2 stuff1 stuff2
>
>
> Do I miss something here?
>

I'd probably design it like this:

cool-script.py --things thing1 thing2 --stuff stuff1 stuff2

which argparse already handles.

ChrisA

Roel Schroeven

unread,
Aug 3, 2021, 7:17:15 PM8/3/21
to
Jon Ribbens via Python-list schreef op 3/08/2021 om 22:55:
> >> Loads of git commands do this. e.g. commit, diff, log, status, etc.
> >> It's not completely unlike what you're describing above, which is
> >> already supported automatically by argparse.
> >
> > Commands like git commit do not use '--' to separate two lists, but as
> > Dan and Sven said to separate options from arguments,
>
> That is not quite correct. Check out 'git diff' for example - it takes
> as position arguments zero, one, or two commit hashes followed by zero
> or more pathnames. You can use '--' to separate these two lists,
> because of course sometimes a pathname can be indistinguishable from
> a commit hash.
Ah yes, I stand corrected, git diff indeed uses -- to separate two lists
(commits and paths).

--
"Too often we hold fast to the cliches of our forebears. We subject all
facts to a prefabricated set of interpretations. Too often we enjoy the
comfort of opinion without the discomfort of thought."
-- John F Kennedy

0 new messages