Code Monkey home page Code Monkey logo

arglite's Introduction

arglite

PyPI version

A lightweight, dynamic argument parsing library for Python programs with klugy support for typing variables.

I made this for a teaching machine project I'm working on (I needed a custom argument parser for reasons), and I'm always too impatient to use argparse.

Installation

Find this tool on PyPI: pip install arglite

Usage

Check this out:

import arglite

def main():
  # Can include explicit requirement
  print(arglite.parser.required.a)
  # Can be an implicit requirement
  print(arglite.parser.b)
  # Can also be purely optional
  print(arglite.parser.optional.c)
  print(arglite.parser.optional.d)

if __name__ == "__main__":
  main()

Run using python main.py -a Yo --b that is -c.

For the more intrepid among us, this also works:

from arglite import parser as cliarg

def main():
  # Can include explicit requirement
  print(cliarg.required.a)
  # Can be an implicit requirement
  print(cliarg.b)
  # Can also be purely optional
  print(cliarg.optional.c)
  print(cliarg.optional.d)

if __name__ == "__main__":
  main()

HELP!

Help now appears when no variables are provided or when requested by use of -h (--h) or -help (--help).

Errors

When errors are present (i.e. flags are provided which aren't used in the code or flags used aren't provided), you'll see errors:

✗ ERROR: A value was provided for A, but the program doesn't call for it
✗ ERROR: A value was expected for a, but not was provided as a flag
✗ ERROR: A value was expected for b, but not was provided as a flag

Notes

  • Flags with no value are automatically converted to True boolean
  • The module uses ast.literal_eval, so "{'a':'b'}" will convert to a dict (all quotes required)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.