Skip to main content

Derek's Argument Parser

Project description

This is a Pythonic argument parser. It automatically converts your sys.argv to *args, **kwargs and passes them to whatever function you wish. It also automatically generates usage messages and handles python type checking / conversions.

For example, if you have the following program:

import darp

def serve(name, port:int=8888):
  '''Example DArP (Derek's Argument Parser) app...'''
  print('running', name, 'on', port)

if __name__=='__main__':

And run it with arguments, you’ll get the following output:

$ python3
Example DArP (Derek's Argument Parser) app...
serve() missing 1 required positional argument: 'name'
usage: python3 <name> [--port <int>]

Required Parameters

*arg parameters are always required. Keyword parameters (like --port) can be marked as required like this:

def serve(name, port:int=darp.REQUIRED):
  print('running', name, 'on', port)

if __name__=='__main__':
  darp.prep(serve, p='port').run()

If run without the required argument this error is printed:

$ python3
serve() missing 1 required argument: '-p|--port'
usage: python3 <name> --port <int>

Shortcut Parameters

Keyword parameters are prefixed with -- (like --port). A shortcut parameter is when you want your script to respond to -p 7777 as if the user typed --port 7777. You specify this relationship via arguments to darp.prep like:

if __name__=='__main__':
  darp.prep(serve, p='port').run()


$ python3 server -p 7777
running server on 7777

Existence Parameters

Keyword parameters without values are considered True if present. For example:

def doit(dry_run=False):
  if dry_run:
    print('doing a dry run...')
    print('doing it for real!')

if __name__=='__main__':

Example run:

$ python3 --dry-run
doing a dry run...

Notice that --dry-run is mapped to the dry_run kwarg.

Existence params can be combined if using shortcuts. See:

def doit(apple=False, banana=False):
  print('apple', apple)
  print('banana', banana)

if __name__=='__main__':
  darp.prep(doit, a='apple', b='banana').run()

Example run:

$ python3 -ab
apple True
banana True

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

darp-1.1.tar.gz (3.4 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page