Skip to main content

Stub a new Python CLI program

Project description

new.py

Python program to write new Python programs

Description

The "new.py" program is intended to automate the creation of a program that uses argparse to handle command-line arguments. Run with -h|--help for the documentation:

usage: new.py [-h] [-n NAME] [-e EMAIL] [-p PURPOSE] [-t] [-f] program

Create Python argparse program

positional arguments:
  program               Program name

optional arguments:
  -h, --help            show this help message and exit
  -n NAME, --name NAME  Name for docstring (default: Ken Youens-Clark)
  -e EMAIL, --email EMAIL
                        Email for docstring (default: kyclark@gmail.com)
  -p PURPOSE, --purpose PURPOSE
                        Purpose for docstring (default: Rock the Casbah)
  -t, --write_test      Create basic test.py (default: False)
  -f, --force           Overwrite existing (default: False)

The only required argument is the "program" name:

$ new.py foo.py
Done, see new script "foo.py."

Now you will have a program that will accept the following parameters:

$ ./foo.py -h
usage: foo.py [-h] [-a str] [-i int] [-f FILE] [-o] str

Rock the Casbah

positional arguments:
  str                   A positional argument

optional arguments:
  -h, --help            show this help message and exit
  -a str, --arg str     A named string argument (default: )
  -i int, --int int     A named integer argument (default: 0)
  -f FILE, --file FILE  A readable file (default: None)
  -o, --on              A boolean flag (default: False)

Edit the "get_args()" function to whatever your program's requirements may be.

If you run this same command again, the "foo.py" file will be detected, and you will be prompted to see if you wish to overwrite it. Unless you answer "yes," the program will exit:

$ new.py foo.py
"foo.py" exists.  Overwrite? [yN] n
Will not overwrite. Bye!

If you do wish to overwrite the program, you can use the -f|--force flag:

$ new.py foo.py -f
Done, see new script "foo.py."

You can indicate the name and email address you wish to appear in the program's docstrings, or you can write a "~/.new.py" configuration file with these parameters, e.g.:

$ cat ~/.new.py
name=Ken Youens-Clark
email=kyclark@gmail.com
purpose=Look out, you rock-and-rollers!

The -p|--purpose option will also become part of the program docstring/argparse documentation. You can also add a "purpose" option to your "~/.new.py" file to override the default "Rock the Casbah" value.

Use the -t|--test option to create a "{prg}_test.py" file and a "Makefile":

$ new.py -t foo.py
Done, see new script "foo.py."

Now you should have the following files:

$ find . -type f
./Makefile
./tests/foo_test.py
./foo.py

You can run "make test" to execute "python3 -m pytest -xv":

$ make test
python3 -m pytest -xv
============================= test session starts ==============================
...

tests/foo_test.py::test_exists PASSED                                    [ 33%]
tests/foo_test.py::test_usage PASSED                                     [ 66%]
tests/foo_test.py::test_ok PASSED                                        [100%]

============================== 3 passed in 0.27s ===============================

Installation

You can copy the new.py program to any directory currently in your $PATH. It's common to place programs into a directory like /usr/local/bin, but this often will require root priviliges. A common workaround is to create a writable directory in your $HOME where you can place programs. I like to use $HOME/.local as the "prefix" for local software installations. This means that $HOME/.local/bin will usually the be location where binaries will be placed; therefore I will add this to my .bash_profile (or .bashrc) file:

export PATH=$HOME/.local/bin:$PATH

See Also

I first created a version of this program for Tiny Python Projects:

https://github.com/kyclark/tiny_python_projects/blob/master/bin/new.py

This version is different in that it incorporates type hints and uses structures like named tuples as records/structs to represent complex, typed objects such as the program "Args".

Chapter 1 of the book covers how to use "new.py" to start a new program. The appendix covers argparse in greater detail.

Author

Ken Youens-Clark kyclark@gmail.com

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

new_py-0.1.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

new_py-0.1.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file new_py-0.1.2.tar.gz.

File metadata

  • Download URL: new_py-0.1.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for new_py-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9177fe10792ace9922daa43339c4f704c0ad757c086ed78f959a1df4230c02cf
MD5 a2cb83ff4e17c1c19740197356223e62
BLAKE2b-256 3b5aa511fbba9d09d7bb293bb83fc243453741c1e5ca5961cf60fde11d7775e5

See more details on using hashes here.

File details

Details for the file new_py-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: new_py-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for new_py-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 81ed6e155e3b6ac00a59de4552216a2bdc8e4a1a1a6549d5dfcb478025436862
MD5 017cc9df494098879c36e96ae2dcace1
BLAKE2b-256 4b57a593fc5aef28637535db4dce3f565ed10a1427d0b20bc99a36b81439e049

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page