Giter Site home page Giter Site logo

bwinkers / argparseprompt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from multimeric/argparseprompt

0.0 1.0 0.0 39 KB

Wrapper for the built-in Argparse, allowing missing command-line arguments to be filled in by the user via interactive prompts

License: GNU General Public License v3.0

Python 100.00%

argparseprompt's Introduction

ArgparsePrompt

ArgparsePrompt is a wrapper for the built-in Argparse, allowing missing command-line arguments to be filled in by the user via interactive prompts

Installation

ArgparsePrompt can be installed using pip:

pip install argparse_prompt

Note that ArgparsePrompt is only compatible with Python 3.6 and higher, and does not support Python 2.X

Usage

Basic Usage

The only public interface of this module is the PromptParser class, which is a subclass of Python's ArgumentParser. Use this class in exactly the same way that you would use ArgumentParser, except that, if any argument does not have a specified default value, and a value is not provided for it on the commandline, the PromptParser will prompt for a value for this argument using input(), which is read from stdin.

Consider the code below (taken from one of the unit tests):

from argparse_prompt import PromptParser

parser = PromptParser()
parser.add_argument('--argument', '-a', help='An argument you could provide', default='foo')
print(parser.parse_args().argument)

If you run this script with a value for argument, the parsing will run as normal:

$ python test/default_parser.py --argument 12
12

However if you don't specify a value for arg, the parser will prompt you for one

$ python test/default_parser.py
argument: An argument you could provide
> (foo) car
car

Default Values

Since this argument has a default value, you can also just hit enter and this value will be used automatically:

python test/default_parser.py
argument: An argument you could provide
> (foo) 
foo

Type Checking

You can also specify a type for the argument in the normal way:

from argparse_prompt import PromptParser

parser = PromptParser()
parser.add_argument('--argument', '-a', help='An argument you could provide', type=int)
print(parser.parse_args().argument)

If you do, this type checking will be used for the value you enter at the prompt:

$ python test/typed_parser.py  
argument: An argument you could provide
abc
Argument "argument" was given a value not of type <class 'int'>

Secure Values

For arguments that need to be secure, such as passwords, secret keys etc, you can use secure=True when defining your argument. This will cause whatever the user inputs for that field to be hidden from the terminal, in the same way that git, or ssh hides the password input.

For example:

parser.add_argument('--password', '-p', help='A very secret password', secure=True)

Situationally Disabling the Prompt

If you use the prompt argument to add_argument, parsing will be disabled:

parser.add_argument('--argument', '-a', help='An argument you could provide', default='foo', prompt=False)

Also, if you want to disable all prompting (for an automated script, for example), just set the ARGPARSE_PROMPT_AUTO to a truthy value:

ARGPARSE_PROMPT_AUTO=1 python test/default_parser.py

argparseprompt's People

Contributors

multimeric avatar bwinkers avatar daemo00 avatar

Watchers

James Cloos avatar

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.