[issue22433] Argparse considers unknown optional arguments with spaces as a known positional argument

2014-09-22 Thread Денис Кореневский

Денис Кореневский added the comment:

There is an standard way to solve this ambiguity.

There is a special marker '--' used to force argument parsing function treat 
all arguments given in command after this marker as positional arguments. It 
was invented specially for tasks where you need to force argument parsing 
engine ignore option indicator and treat argument as positional.

I know this argument as standard 'de facto', but can't find any documents about 
'--' interpretation.
But you can see, that Linux 'libc' standard C library function getopt() knows 
about this flag:
http://linux.die.net/man/3/getopt
So, any utility using this function knows about '--'.

For example: grep, tee, cat, tail, head, vim, less, rm, rmdir, common shells 
(sh, bash, csh, dash, ...) and so on. The list is large. Almost any utility 
except special ones like 'echo' which main function is to print any argument 
given to utility.

So, I think, that the true way here is to parse all arguments staring with 
'-'/'--' and listed before special '--' marker as optional, and treat all of 
arguments listed after '--' as positional ones and do not try to use any 
secondary indicators to make a decision: 'optional' or not.


For example:

any parameter before '--' special marker starting with '--' is treated as 
optional
# python3 example.py --bar=one two xxx
(Namespace(pos='xxx'), ['--bar=one two'])

all parameters after '--' special marker are treated as positional
# python3 example.py -- --bar=one two xxx
(Namespace(pos='--bar=one two'), ['xxx'])


Yes, my patch does not the solution and argparse should be patched another way.

Where and how can I get unittests for argparse module to check my code 
modifications before posting them here? I want to try to modify argparse and 
produce the patch changing argparse behaviour as described above.

Thanks.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22433
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22433] Argparse considers unknown optional arguments with spaces as a known positional argument

2014-09-17 Thread Денис Кореневский

New submission from Денис Кореневский:

Argparse version 1.1 consider ANY unknown argument string containig ' ' (space 
character) as positional argument. As a result it can use such unknown optional 
argument as a value of known positional argument.

Demonstration code:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument(--known-optional-arg, -k, action=store_true)
parser.add_argument(known_positional, action=store, type=str)
parser.parse_known_args([--known-optional-arg, --unknown-optional-arg=with 
spaces, known positional arg])
parser.parse_known_args([--known-optional-arg, 
--unknown-optional-arg=without_spaces, known positional arg])

Bugfix is attached to issue and affects ArgumentParser._parse_optional() method 
body.

Sorry, if it is a better way to report (or, possibly, fix) a bug than this 
place. It is my first python bug report.

Thanks.

--
components: Extension Modules
files: argparse.py.patch
keywords: patch
messages: 227007
nosy: DenKoren
priority: normal
severity: normal
status: open
title: Argparse considers unknown optional arguments with spaces as a known 
positional argument
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file36641/argparse.py.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22433
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22433] Argparse considers unknown optional arguments with spaces as a known positional argument

2014-09-17 Thread Денис Кореневский

Денис Кореневский added the comment:

I've signed 'Contributor's Agreement'.

Bug demonstration code listed in issue description is in file attached to 
comment.

It can be run with following command:
python argparse_bug_example.py

--
Added file: http://bugs.python.org/file36643/argparse_bug_example.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22433
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com