3 ms·
import argparse parser = argparse.ArgumentParser() parser.add_argument('-v','--version',action='version', version='demo',help='Print version information
by mey 6y ago
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v','--version',action='version', version='demo',help='Print version information')
parser.add_argument('-d','--debug', help='Enable Debug Mode')
parser.add_argument('a','arg', help="Argument Documentation")
args = parser.parse_args()
Personally I feel like this is more readable code, gets me better validation, and help docs for "free". That's the attraction.
- gitgud 6y agoElegant, but then it's no longer a basic shell-script as it requires python installed. If you can live with additional dependencies, then I like the node [1] commander package, which is very readable and nice to work with in my opinion. #!/usr/bin/env node const { program } = require('commander'); program .command('clone <source> [destination]') .description('clone a repository') .action((source, destination) => { console.log('clone command called'); }); It also automatically generates the --help output for ./script -h [1] https://github.com/tj/commander.js/ https://github.com/tj/commander.js/