In short:
- Fix all deprecation warnings.
- If you extended argparse classes - propagate appropriate changes.
For example:
argparse.ArgumentParser({ addHelp: false })
->argparse.ArgumentParser({ add_help: false })
parser.printHelp()
->parser.print_help()
parser.add_argument({ action: 'storeTrue' })
->parser.add_argument({ action: 'store_true' })
Old names still have aliases (with deprecation messages), and your code may work. But no guarantees, especially if you extend classes.
Old names still has aliases with deprecaion messages, to simplify migration.
parser.add_argument('-h', '--help', { help: 'show this help message and exit' })
Old signature is supported but shows deprecation message.
Override .exit()
method instead.
const argparse = require('argparse')
class MyArgumentParser extends argparse.ArgumentParser {
exit() { console.log('no exiting today') }
}
parser = new MyArgumentParser()
Use version
action instead:
parser.add_argument('-v', '--version', { action: 'version', version: '1.0.0' })
parser.add_argument('--foo', { type: 'str' })
Old signature is supported but shows deprecation message.
If user input is invalid, throw TypeError instead of Error:
parser.add_argument('--digit', {
type: function digit(v) {
if (!/^\d$/.test(v)) throw TypeError('not a digit')
return +v
}
})
TypeErrors will get intercepted and turned into user-friendly error messages, but ordinary Errors will not.
Constants SUPPRESS
, OPTIONAL
, ZERO_OR_MORE
, ONE_OR_MORE
, PARSER
,
and REMAINDER
previously available as argparse.Const.*
are renamed to argparse.*
.
Constant _UNRECOGNIZED_ARGS_ATTR
is no longer exposed publicly.
Constant EOL
no longer exists (hardcoded as '\n') - replace with '\n' if you used it somewhere.
Get values from Namespace
as if it was a plain js object.
- if you passed
null
to any of the functions, it will be treated as a value (not replaced by default) parse_args
will return{ x: undefined }
instead of{ x: null }
if optional arg isn't specified