GithubHelp home page GithubHelp logo

uchicago-library / argandenvparse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bnbalsamo/argandenvparse

0.0 4.0 0.0 20 KB

Extends argparse.ArgumentParser to optionally read values from environmental variables

License: GNU General Public License v3.0

Python 97.52% Shell 2.48%

argandenvparse's Introduction

argandenvparse

v0.0.1

Build Status Coverage Status

A patch for argparse which gives the ArgumentParser class the ability to parse environmental variables as well

If an environmental variable in all caps beginning with a given prefix is found, the environmental variable name after the prefix (in all lowercase) is used as the key, and the value of the environmental variable as the value for the resulting args Namespace, assuming you have added an argument with either a dest or an option string that matches the content of the environmental variable after the prefix.

(For a demonstration of this functionality see the code snippet below.)

This is implemented in a new method: ArgumentParser.parse_args_and_env()

This method works similarly to argparse.ArgumentParser.parse_args() with the following notable exceptions:

  • One required position argument: a prefix to search for in environmental arguments, the value following the prefix in the environmental variable name will be used as the key in the resulting Namespace.
  • Two new keyword arguments
    • enable_positional (True): Enable parsing positional arguments from the environment.
      • Note that these are not clobbered by CLI input, but rather CLI input is appended as an additional positional argument
    • error_on_unrecognized_env_vars (True): Finding environmetal variables that contain the prefix but are not referenced in the parser will be silently ignored if this is set to false, otherwise behavior is exactly the same as passing unrecognized arguments on the CLI
$ EXAMPLE_FOO=bar python
>>> from argandenvparse import ArgumentParser
>>> parser = ArgumentParser()
>>> parser.add_argument('--foo')
>>> parser.parse_args_and_env('EXAMPLE_')
Namespace(foo='bar')
>>> parser.parse_args_and_env('EXAMPLE_', args=['--foo', 'baz'])
Namespace(foo='baz')

Author

Brian Balsamo [email protected]

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.