2

I'm trying to read some hex values i need for my pythonscript using argparser, an example input looks like:

parser.add_argument("start", type=hex, help="hex PC address you want to start recording data")

The problem occurs when i try to start the script with a hexvalue. example:

python testscript.py 0x21c

Then ik get error:

 error: argument start: invalid hex value: '0x21c'

is also tried following values: 0X21C, 21C, 21c, 21. All of them result in the same error.

EDIT: i don't nee the hex value converted to a int. I just need the hex value i inputted. Its a PC address where a µC needs to jump to.

Does anyone know what i'm doing wrong here? Thanks!

Michvw
  • 329
  • 1
  • 4
  • 12
  • `hex` takes an integer and turns it into a string. `argparse` passes a string and stores the transformed result. What type do you want `args.start` to be? – mgilson May 03 '16 at 14:06
  • Possible duplicate of [Python argparse fails to parse hex formatting to int type](http://stackoverflow.com/questions/25513043/python-argparse-fails-to-parse-hex-formatting-to-int-type) – Robin Davis May 03 '16 at 14:07
  • hex ofcourse, thats why i specified type=hex, if hex takes an integer is doesn't explain why it crashed when i insert 21 and that would give a problem when i want to give 21c as an argument – Michvw May 03 '16 at 14:18
  • 1
    @Michvw -- `argparse` will always pass a string to the `type` function (since it's a string on the commandline). What are you looking to get out of the `hex` function? You're already passing hex strings from the command-line. Are you using `hex` to validate the strings? Or do you expect it to convert the strings `'0x21c` to an integer? – mgilson May 03 '16 at 15:10

1 Answers1

5

Inspired by:

Convert hex string to int in Python

I tried

In [471]: parser=argparse.ArgumentParser()

In [472]: parser.add_argument('ahex',type=lambda x: hex(int(x,0)))

In [473]: parser.parse_args(['0x21c'])  # valid hex string input
Out[473]: Namespace(ahex='0x21c')

In [474]: parser.parse_args(['21'])  # converts valid int to hex string
Out[474]: Namespace(ahex='0x15')

In [475]: parser.parse_args(['21c'])   # error if string is not valid hex

usage: ipython3 [-h] ahex
ipython3: error: argument ahex: invalid <lambda> value: '21c'

As @mgilson stressed in the comments, the type parameter is a function, one that takes a string and returns something. It also raises an error if the string is not 'valid'. hex() does not work as type, because it takes an integer and returns a hex string. hex('0x21c') is not valid use of that function.

For this quick-n-dirty solution I used a lambda function. It could just as well been a def. I used int(x,0) to convert the string to a int, in a way that handles both hex strings and integer strings. Then I converted that integer back to a string using the hex function.

So the net effect of my lambda is to just validate the hex string. If a valid string it just returns the same thing (same as if type was the default identity lambda x: x).

Confusion over the nature of the type parameter arises most often when people want a boolean value. bool() does not take a string like 'True' or 'False' and return a boolean value. Look up those SO questions if the issue is still confusing.

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • 1
    I somehow expected that the argparse library would take care of the conversion. Thank you for this explanation! – Michvw May 04 '16 at 06:47
  • From the Python documentation on `int`: Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16. – Hendrikto Dec 06 '17 at 21:28