1

I have seen Format ints into string of hex - but I simply cannot figure out how to apply that here:

import argparse

def parse_args(args):
  parser = argparse.ArgumentParser(description="Hello hex")
  # use lambda to allow for hex parsing https://stackoverflow.com/q/25513043
  parser.add_argument('--my_hex', type=lambda x: int(x,0), default=12342, help="set a hex number (default: %(default)s)")
  return parser.parse_args(args)

def main(inargs):
  args = parse_args(inargs)

If I call this:

$ python3 ../my_hex_script.py --help
usage: my_hex_script.py [-h] [--my_hex MY_HEX]

Hello hex

options:
  -h, --help       show this help message and exit
  --my_hex MY_HEX  set a hex number (default: 12342)

... clearly the printout of default value of my_hex is in decimal.

How do I get it printed in 0x{:04X} string format?

sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • Is the default value of the int you want `12342`? You just want the default value displayed in a particular way? – juanpa.arrivillaga Feb 02 '23 at 20:33
  • 4
    `%(default)s` -> `%(default)#04x` – njzk2 Feb 02 '23 at 20:34
  • surely `int(x,0)` is meant to be `int(x,16)`? – njzk2 Feb 02 '23 at 20:35
  • 4
    @njzk2 I had to look that up, since obviously base 0 makes no sense... but basically it means "parse it like a source code literal" so it will accept any string that is a valid `int` literal. So `int("0o320", 0)` and `int("0xd0", 0)` both get parsed to `208` – juanpa.arrivillaga Feb 02 '23 at 20:37
  • Alternatively, you can use `default="0x3036"`. But `my_hex` is a weird name for an `int` value – juanpa.arrivillaga Feb 02 '23 at 20:41
  • 1
    Many thanks @njzk2 - `%(default)#04x` works great, feel free to post as an answer, I'll accept it – sdbbs Feb 02 '23 at 20:41
  • Thanks @juanpa.arrivillaga - but `default="0x3036"` is a string; the name is just arbitrary for the sake of example; what I wanted to achieve is `%(default)#04x` -> then `0x3036` is printed in the help string, which is the hex format of 12342 (and I keep the value as int). – sdbbs Feb 02 '23 at 20:43
  • 3
    @sdbbs so what if it is a string? it will get parsed into an `int` by your function. I agree with the formatting approach though (I've never used the argparse help formatting before!) – juanpa.arrivillaga Feb 02 '23 at 20:44
  • @juanpa.arrivillaga - sure, but I come to Python on and off, and if after a couple of months, after I've forgotten all of this :), I come back to my script, and see a string (quotes) around something I intend to be an int from the start (but which otherwise is referred to in its hex format), then it will give me extra confusion, until I find this question and refresh my memory :) LIke this, with hex format directly in the formatting string, I won't have that problem :) – sdbbs Feb 02 '23 at 20:47

1 Answers1

0

Converting @njzk2's comment into an answer so everyone knows this is solved:

Final code should switch %(default)s to %(default)#04x. Looks like:

import argparse

def parse_args(args):
  parser = argparse.ArgumentParser(description="Hello hex")
  # use lambda to allow for hex parsing https://stackoverflow.com/q/25513043
  parser.add_argument('--my_hex', type=lambda x: int(x,0), default=12342, help="set a hex number (default: %(default)#04x)")
  return parser.parse_args(args)

def main(inargs):
  args = parse_args(inargs)
BLimitless
  • 2,060
  • 5
  • 17
  • 32