Skip to content

Instantly share code, notes, and snippets.

@evilchili
Last active August 2, 2018 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilchili/e54eb32d4090ab249cd67432f95f8dc7 to your computer and use it in GitHub Desktop.
Save evilchili/e54eb32d4090ab249cd67432f95f8dc7 to your computer and use it in GitHub Desktop.
CLI with Fire
from fire import Fire as _Fire
import sys
def Fire(target):
"""
Changing Log Levels:
To manage the logging level, you can set the LOGLEVEL environment variable before running the command.
Supported values are DEBUG, CRITICAL, ERROR, WARNING, and INFO (the default). For example:
LOGLEVEL=DEBUG {script} [subcomand] [parameter..]
About MyPackage
---------------
The MyPackage CLI is procedurally generated. Most tools include one or more subcommands, and you can view
help documentation for each one. To view help for a specific command, try:
{script} subcommand -- --help
This documentation is also available online at:
https://link.to.docs.site/
We have made every attempt to generate tools that are safe, predictable, and easy to use. If this is not your
experience, please report a bug or make a feature request using this link:
https://support.request.site/
"""
try:
target.__doc__ += Fire.__doc__.format(script=sys.argv[0])
_Fire(target)
# catch common failures with common remediation actions
except SignOnFailureException:
print("Do you need to refresh your credentials?")
raise SystemExit(1)
except HealthCheckException:
print("It isn't safe to run this command right now because reasons.")
#!/bin/env python
from lib import someservice
someservice.run()
from cli import Fire
class SomeServiceCLI:
"""some-service
Command:
some-service -- a tool for interacting with SomeService
Description:
This tool implements a command-line interface for SomeService.
"""
def __init__(self):
self._service = SomeServiceHelper()
def is_alive(self, ipv6=True, ipv4=True):
"""some-service is-alive
Command:
some-service is-alive -- Determine if SomeService is responding on at least one interface.
Description:
This tool will print "True" if the service is responding to ping requests, or "False" otherwise.
Examples:
% some-service is-alive
True
% some-service is-alive ipv6=False
False
% some-service is-alive ipv4=True ipv6=False
True
Options:
IPV6 Set to False to skip checking the IPv6 interface
IPV4 Set to False to skip checking the IPv4 interface
"""
if not (ipv6 or ipv4):
raise RuntimeError("You must include at least one interface; see some-service is-alive -- --help")
if ipv6 and self._service.ping6():
return True
elif ipv4 and self._service.ping():
return True
return False
def run():
Fire(SomeServiceCLI())
@evilchili
Copy link
Author

% some-service
Type:        CLI
String form: <lib.someservice.SomeServiceCLI object at 0x7f31a5afdba8>
Docstring:   some-service

    Command:
        some-service -- a tool for interacting with SomeService

    Description:
        This tool implements a command-line interface for SomeService.

    Changing Log Levels:
        To manage the logging level, you can set the LOGLEVEL environment variable before running the command.
        Supported values are DEBUG, CRITICAL, ERROR, WARNING, and INFO (the default). For example:
            LOGLEVEL=DEBUG some-service [subcomand] [parameter..]

    About MyPackage
    ---------------
    The MyPackage CLI is procedurally generated. Most tools include one or more subcommands, and you can view
    help documentation for each one. To view help for a specific command, try:
        some-service subcommand -- --help

    This documentation is also available online at:
        https://link.to.docs.site/

    We have made every attempt to generate tools that are safe, predictable, and easy to use. If this is not your
    experience, please report a bug or make a feature request using this link:
        https://support.request.site/

Usage:    some-service
          some-service is-alive

@evilchili
Copy link
Author

evilchili commented Aug 2, 2018

% some-service is-alive --help
Type:        CLI
String form: <lib.someservice.SomeServiceCLI object at 0x7f31a5afdba8>
Docstring:   some-service is-alive

Command:
    some-service is-alive -- Determine if SomeService is responding on at least one interface.

Description:
    This tool will print "True" if the service is responding to ping requests, or "False" otherwise.

Examples:

    % some-service is-alive
    True

    % some-service is-alive ipv6=False
    False

    % some-service is-alive ipv4=True ipv6=False
    True

Changing Log Levels:
    To manage the logging level, you can set the LOGLEVEL environment variable before running the command.
    Supported values are DEBUG, CRITICAL, ERROR, WARNING, and INFO (the default). For example:
            LOGLEVEL=DEBUG some-service [subcomand] [parameter..]

About MyPackage
---------------
The MyPackage CLI is procedurally generated. Most tools include one or more subcommands, and you can view
help documentation for each one. To view help for a specific command, try:
    some-service subcommand -- --help

This documentation is also available online at:
    https://link.to.docs.site/

We have made every attempt to generate tools that are safe, predictable, and easy to use. If this is not your
experience, please report a bug or make a feature request using this link:
    https://support.request.site/

Options:
IPV6    Set to False to skip checking the IPv6 interface
IPV4    Set to False to skip checking the IPv4 interface

Usage:    some-service is-alive [IPV6] [IPV4]
          some-service is-alive [--ipv6 IPV6] [--ipv4 IPV4]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment