Last active
August 2, 2018 22:50
-
-
Save evilchili/e54eb32d4090ab249cd67432f95f8dc7 to your computer and use it in GitHub Desktop.
CLI with Fire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
from lib import someservice | |
someservice.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Author
evilchili
commented
Aug 2, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment