Skip to content

Instantly share code, notes, and snippets.

@st4lk
Created November 22, 2022 15:03
Show Gist options
  • Save st4lk/195ec0e4eba45cc6fa7e704b6f6f3a2e to your computer and use it in GitHub Desktop.
Save st4lk/195ec0e4eba45cc6fa7e704b6f6f3a2e to your computer and use it in GitHub Desktop.
python argparse example
import argparse
parser = argparse.ArgumentParser(description='Argparse example command')
parser.add_argument('just_string', help='Positional required string argument')
parser.add_argument(
'--flag', action='store_true', help='Not required boolean argument with default value',
)
parser.add_argument(
'--value', type=str, default='ok', help='Not required string argument with default value',
)
parser.add_argument(
'--ids', nargs='+', default=['1', '2'], help='List of ids: "--ids 1 2 3',
)
parser.add_argument(
'--csv_file', type=str, help='Path to csv, probably you want ergeon/calc/data/values.csv',
)
# parser.add_argument(
# nargs='+', dest='keys', help='Any number of args without name, at least one is required',
# )
if __name__ == '__main__':
args = parser.parse_args()
print('just_string: {0}'.format(args.just_string))
print('flag: {0}'.format(args.flag))
print('value: {0}'.format(args.value))
print('ids: {0}'.format(args.ids))
print('csv_file: {0}'.format(args.csv_file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment