Skip to content

Instantly share code, notes, and snippets.

@ashishmadeti
Created October 27, 2021 08:51
Show Gist options
  • Save ashishmadeti/cccc77f5c50ea4ed722054580d1aaa17 to your computer and use it in GitHub Desktop.
Save ashishmadeti/cccc77f5c50ea4ed722054580d1aaa17 to your computer and use it in GitHub Desktop.
Boilerplate code for accepting and parsing command line arguments in Python and Javascript
#!/usr/bin/python3
import argparse
parser = argparse.ArgumentParser(description="Grep for JSON log files")
parser.add_argument('pattern', help='pattern to search for in each line')
parser.add_argument('file', help='file to search the pattern in')
parser.add_argument('-k', help='search only in keys of the JSON object', action="store_true")
# Implement rest of the command line flags here
args = parser.parse_args()
file = args.file
pattern = args.pattern
search_only_in_keys = args.k
#!/usr/bin/env node
const yargs = require('yargs')
const argv = yargs.command({
command: '$0 <pattern> <file>',
describe: 'Grep for JSON log files',
builder: {
k: {
describe: 'search only in keys of the JSON object',
type: 'boolean'
}
// Implement rest of the command line flags here
}
}).help('h').alias('help', 'h').argv
let fileName = argv.file
let pattern = argv.pattern
const search_only_in_keys = argv.k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment