python - optparse: Support additional prefixes on integer arguments? -


optparse supports '0b', '0' , '0x' prefixes on integer arguments signify binary, octal , hexadecimal respectively. need support additional prefixes, including '%' binary , '$' hex.

one way patch optparse._parse_num, follows:

oldparsenum = optparse._parse_num def newparsenum(val, type):     val = re.sub('^%', '0b', val)     val = re.sub('^\$', '0x', val)     return oldparsenum(val, type) optparse._parse_num = newparsenum 

although work, seems quite fragile. there better approach?

you define own option-handler/-callback:

parser.add_option("-c", action="callback", callback=my_callback) 

to handle special cases or extend syntax.

take documentation determine if sufficient , better (monkey-)patching.


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

sql - TSQL XML Parsing / Insert -

c# - Why won't my code write to SQL? -