You'd normally have the shell formulate the exact bytes, but since you cannot pass in NUL bytes as arguments asking users to pass in escape sequences is a reasonable work-around.
However, the shell is not going to interpret Python byte string literal notation.
In this case, I'd ask the user to enter hexadecimal values instead:
python myprogram.py -d "64 00 21 00 57 00"
and use the binascii.unhexlify()
function to produce your bytes
value from that (removing any whitespace first):
whitespace = dict.fromkeys((9, 10, 13, 32)) # tab, space, newline and carriage return
data = binascii.unhexlify(args.data.translate(whitespace))
This does require that you set your default argument value to a compatible value:
parser.add_argument("-d", "--data", default='00')
The alternative would be to use the ast.literal_eval()
function to interpret the Python byte string literal syntax:
data = ast.literal_eval(args.data)
and your default'd be:
parser.add_argument("-d", "--data", default=repr(b'\0'))
but take into account that this function accepts any Python literal, so you could end up with any other object type, including numbers, strings and containers.