The argparse library handles escaped characters (like \t to tab and \n to newline) differently than I prefer. An answer to this question gives a solution but I would like to make it less visible to the user.
Given the program:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--delimiter', default='\t')
args = parser.parse_args()
print(args)
You will receive this output:
bash$ parser.py -d \t
Namespace(delimiter='t')
bash$ parser.py -d \\t
Namespace(delimiter='\\t')
bash$ parser.py -d '\t'
Namespace(delimiter='\\t')
bash$ parser.py -d '\\t'
Namespace(delimiter='\\\\t')
bash$ parser.py -d "\t"
Namespace(delimiter='\\t')
bash$ parser.py -d "\\t"
Namespace(delimiter='\\t')
bash$ parser.py -d $'\t'
Namespace(delimiter='\t')
bash$ parser.py -d $'\\t'
Namespace(delimiter='\\t')
bash$ parser.py -d $"\t"
Namespace(delimiter='$\\t')
bash$ parser.py -d $"\\t"
Namespace(delimiter='$\\t')
I get the desired argument only with
parser.py -d $'\t'
but I would prefer the input to look something like
parser.py -d \t
or less preferably
parser.py -d '\t'
parser.py -d "\t"
If I want to change the behavior, is this something I can do using the argparse library? If not, is it possible for me to write the behavior on top of the existing argparse library? If not, is this just the way that bash passes arguments to argparse therefore out of my hands? If that is true, is this something that is usually documented to users or is this behavior assumed to be normal?