Python2.7 argparse.parse_known_args parsing incorrectly

Erin

background I have a script where I am using parseargs to partially parse the input.

parser = argparse.ArgumentParser()
parser.add_argument("-c", action="store_true")
nspc = parser.parse_known_args()

The reason for using parseargs is to allow the -c option to occur anywhere in the input. I only want to parse this option, and then later in the code I do additional parsing on my own. The reason for this is, the script accepts a large number of arguments from a configuration file, ie, myscript.py doesn't know the arguments until runtime.

problem The problem I am running into is, some of the arguments may start with the letter "c.", so if I say

$ myscript.py "some argument" -cdef

I get the error

myscript.py: error: argument -c: ignored explicit argument 'ef'

Since I said parser.add_argument("-c", ... I would expect that -c would be parsed but -cdef would not. Obviously, -c is an optional argument.

How can I tell the parser that -c is an option but -cdef is not?

Amber

Use -- for multi-character argument names.

It's fairly common in UNIX for -cdef to mean -c -d -e -f, whereas --cdef is a single option. argparse follows this convention.

(As an example: ls -la is the equivalent of ls -l -a, whereas ls --color is just a single option.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Understanding argument parsing with argparse in Python

From Dev

Argument parsing python using ArgParse

From Dev

Python argparse: Complex Argument Parsing Scenario

From Dev

python argparse stops parsing after it encounters '$'

From Dev

python argparse extra args

From Dev

Is AngularJS parsing the value incorrectly?

From Dev

SimpleDateFormat incorrectly parsing string

From Dev

MomentJS: Date parsing incorrectly

From Dev

Liquid parsing date incorrectly

From Dev

Bodyparser is incorrectly parsing JSON?

From Dev

Issue with Python 3.4.2 and csv.DictReader parsing the first fieldname incorrectly

From Dev

Python argparse parse_args into global namespace (or a reason this is a bad idea)

From Dev

Python argparse parse_args into global namespace (or a reason this is a bad idea)

From Java

Parsing boolean values with argparse

From Dev

Python argparse to ignore concatenated args?

From Dev

SQL Server Incorrectly Parsing Date

From Dev

ArgumentParser -h not printing options after parse_known_args

From Dev

ArgumentParser -h not printing options after parse_known_args

From Dev

Testing arguments parsing with unittest and argparse

From Dev

python argparse, how to refer args by their name

From Dev

How do I create a Python namespace (argparse.parse_args value)?

From Dev

Use argparse module in python to parse a single argument

From Dev

Python argparse does not parse images with wildcard

From Dev

PageDown through ScriptEngine incorrectly parsing Markdown

From Dev

angular.js parsing integer incorrectly

From Dev

Command line arguments, paths with spaces, parsing incorrectly

From Dev

Dynamic MS SQL Variable Parsing incorrectly

From Dev

Why is NSJSONSerialization parsing NSDictionary incorrectly into JSON?

From Dev

Json parser, incorrectly parsing string as a number

Related Related

  1. 1

    Understanding argument parsing with argparse in Python

  2. 2

    Argument parsing python using ArgParse

  3. 3

    Python argparse: Complex Argument Parsing Scenario

  4. 4

    python argparse stops parsing after it encounters '$'

  5. 5

    python argparse extra args

  6. 6

    Is AngularJS parsing the value incorrectly?

  7. 7

    SimpleDateFormat incorrectly parsing string

  8. 8

    MomentJS: Date parsing incorrectly

  9. 9

    Liquid parsing date incorrectly

  10. 10

    Bodyparser is incorrectly parsing JSON?

  11. 11

    Issue with Python 3.4.2 and csv.DictReader parsing the first fieldname incorrectly

  12. 12

    Python argparse parse_args into global namespace (or a reason this is a bad idea)

  13. 13

    Python argparse parse_args into global namespace (or a reason this is a bad idea)

  14. 14

    Parsing boolean values with argparse

  15. 15

    Python argparse to ignore concatenated args?

  16. 16

    SQL Server Incorrectly Parsing Date

  17. 17

    ArgumentParser -h not printing options after parse_known_args

  18. 18

    ArgumentParser -h not printing options after parse_known_args

  19. 19

    Testing arguments parsing with unittest and argparse

  20. 20

    python argparse, how to refer args by their name

  21. 21

    How do I create a Python namespace (argparse.parse_args value)?

  22. 22

    Use argparse module in python to parse a single argument

  23. 23

    Python argparse does not parse images with wildcard

  24. 24

    PageDown through ScriptEngine incorrectly parsing Markdown

  25. 25

    angular.js parsing integer incorrectly

  26. 26

    Command line arguments, paths with spaces, parsing incorrectly

  27. 27

    Dynamic MS SQL Variable Parsing incorrectly

  28. 28

    Why is NSJSONSerialization parsing NSDictionary incorrectly into JSON?

  29. 29

    Json parser, incorrectly parsing string as a number

HotTag

Archive