argparse: delimiter for argparse list arguments

Roel Schroeven roel at roelschroeven.net
Tue Aug 3 14:54:28 EDT 2021


Jon Ribbens via Python-list schreef op 3/08/2021 om 17:48:
> On 2021-08-03, Michael Torrie <torriem at 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, 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



More information about the Python-list mailing list