All Projects → Jaymon → Captain

Jaymon / Captain

Licence: mit
command line python scripts for humans

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Captain

Carbon Now Cli
🎨 Beautiful images of your code — from right inside your terminal.
Stars: ✭ 5,165 (+51550%)
Mutual labels:  cli-app, cli-utilities, cli, cli-command
Rust Cli Boilerplate
Rust project boilerplate for CLI applications
Stars: ✭ 108 (+980%)
Mutual labels:  cli-app, cli, cli-command
F License
Open Source License Key Generation and Verification Tool written in Go
Stars: ✭ 535 (+5250%)
Mutual labels:  cli-app, cli
What Anime Cli
❓🖼 Find the anime scene by image using your terminal
Stars: ✭ 533 (+5230%)
Mutual labels:  cli-app, cli
Tml
🌈💻🎨 A tiny markup language for terminal output. Makes formatting output in CLI apps easier!
Stars: ✭ 634 (+6240%)
Mutual labels:  cli-app, cli
Rust Sloth
A 3D software rasterizer... for the terminal!
Stars: ✭ 478 (+4680%)
Mutual labels:  cli-app, cli
Dark Mode
Control the macOS dark mode from the command-line
Stars: ✭ 518 (+5080%)
Mutual labels:  cli-app, cli
Solidarity
Solidarity is an environment checker for project dependencies across multiple machines.
Stars: ✭ 540 (+5300%)
Mutual labels:  cli-app, cli
Yaspin
A lightweight terminal spinner for Python with safe pipes and redirects 🎁
Stars: ✭ 413 (+4030%)
Mutual labels:  cli-utilities, cli
Fkill Cli
Fabulously kill processes. Cross-platform.
Stars: ✭ 6,418 (+64080%)
Mutual labels:  cli-app, cli
Sade
Smooth (CLI) Operator 🎶
Stars: ✭ 746 (+7360%)
Mutual labels:  cli-app, cli
Archisteamfarm
C# application with primary purpose of idling Steam cards from multiple accounts simultaneously.
Stars: ✭ 7,219 (+72090%)
Mutual labels:  cli-app, cli
Macos Wallpaper
Manage the desktop wallpaper on macOS
Stars: ✭ 450 (+4400%)
Mutual labels:  cli-app, cli
Cobra
A Commander for modern Go CLI interactions
Stars: ✭ 24,437 (+244270%)
Mutual labels:  cli-app, cli
Ttyplot
a realtime plotting utility for terminal/console with data input from stdin
Stars: ✭ 532 (+5220%)
Mutual labels:  cli-app, cli
Npm Run All
A CLI tool to run multiple npm-scripts in parallel or sequential.
Stars: ✭ 4,496 (+44860%)
Mutual labels:  cli, cli-command
Initior
A command line application that let's you initialize your new projects the right way, replaces npm and yarn's init 🎆
Stars: ✭ 17 (+70%)
Mutual labels:  cli-app, cli
Ava
Node.js test runner that lets you develop with confidence 🚀
Stars: ✭ 19,458 (+194480%)
Mutual labels:  cli-app, cli
Cpx
A cli tool to watch and copy file globs.
Stars: ✭ 394 (+3840%)
Mutual labels:  cli, cli-command
Np
A better `npm publish`
Stars: ✭ 6,401 (+63910%)
Mutual labels:  cli-app, cli

Captain

Easy python cli scripts for people that just want get things done.

Usage

A valid captain cli script needs just two things:

  1. A main function

    def main(foo, bar):
        return 0
    
  2. Calling exit using captain.exit(__name__)

    import captain
    
    def main(foo, bar):
        return 0
    
    captain.exit(__name__)
    

That's it! Whatever arguments you define in the main function will be options on the command line. A captain script is called just like any other python command line script, so to run the above example you could do:

$ python path/to/script.py --foo=1 --bar=2

Argument Decorator

The captain.decorators.arg() decorator provides a nice passthrough api to the full argparse module if you need to really customize how arguments are passed into your script:

from captain import exit
from captain import echo
from captain.decorators import arg 


@arg('--foo', '-f')
@arg('arg', metavar='ARG')
def main(*args, **kwargs):
    '''this is the help description'''
    echo.out(args)
    echo.out(kwargs)

exit(__name__)

Would print a help string like this:

usage: script.py [-h] [--foo FOO] ARG

this is the help description

positional arguments:
  ARG

optional arguments:
  -h, --help         show this help message and exit
  --foo FOO, -f FOO

If you want another nifty way to define arguments, take a look at docopt.

Echo

This small module makes it easy to print stuff in your script while still giving you full control by being able to configure the logger if you need to. It also will obey the global --quiet flag.

from captain import echo

var1 = "print"

var2 = "stdout"
echo.out("this will {} to {}", var1, var2)

var2 = "stderr"
echo.err("this will {} to {}", var1, var2)

e = ValueError("this will print with stacktrace and everything")
echo.exception(e)

The echo module has a lot of nice little helper features but Captain also can work with modules like clint if you need to do more advanced cli output.

Examples

A typical standard python cli script

import argparse

if __name__ == u'__main__':
    parser = argparse.ArgumentParser(description='fancy script description')
    parser.add_argument("--foo", action='store_true')
    parser.add_argument("--bar", default=0, type=int)
    parser.add_argument("args", nargs='*')
    args = parser.parse_args()

would become:

import captain

def main(foo=False, bar=0, *args):
    '''fancy script description'''
    return 0

captain.exit(__name__)

Subcommands

Captain supports multiple subcommands defined in the script using the format main_subcommand.

# cli.py

import captain

def main_foo():
    pass

def main_bar():
    pass

captain.exit(__name__)

So foo could be called using:

$ python cli.py foo

And bar could be called using:

$ python cli.py bar

Embedding captain in another package

If you want a script from you package to be usable using both python -m example and maybe a console_scripts entry point, you can set up your package's __main__.py module like this:

# example/__main__.py

from captain import exit

def main():
    pass

# hook for setup.py entry_points
def console():
    exit(__name__)
    
# hook for python -m MODULE call
if __name__ == "__main__":
    console()

And then in your setup.py script you can add:

entry_points = {
    'console_scripts': [
        'example = example.__main__:console'
    ],
}

That's all there is to it.

Easy listing of all captain scripts in a directory

You can get a list of all available scripts in a directory by running captain with no arguments:

$ captain

Install

Use pip:

$ pip install captain

For latest and greatest:

$ pip install --upgrade "git+https://github.com/Jaymon/captain#egg=captain"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].