Chepy CLI¶
Chepy CLI is a fully dynamically generated cli that combines the power for python-fire and prompt_toolkit to create its cli. The cli is used very similar to how the main Chepy
class is used as it allows for method chaining. We know from the docs that some of the methods in the Chepy
class takes optional or required arguments. In the cli, these are passed as flags. Refer to the examples for use cases.
Cli options¶
usage: chepy [-h] [-v] [-r RECIPE] [data [data ...]]
positional arguments:
data
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-r RECIPE, --recipe RECIPE
Cli magic markers¶
There are three special characters in the cli.
!
If a line begins with!
it is interpreted as a shell command. For example:! ls -la
#
If a line begins with#
it is interpreted as a comment. For example:# hello
?
If a line begins with?
it provides help for the provided Chepy method. For example:? to_hex
Cli Recipes¶
The cli can be run in a non interactive mode using the -r
flag. This flag will load the recipe that is supplied and run it on the arguments that is provided.
# Example
chepy -r test.recipe a.png
Using builtins¶
One of the more advanced functions of the cli allows the user to use arbitrary builtin methods when the state does not contain a Chepy object.
Consider this example in code. We will parse a User agent string in this case:
>>> ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
>>> c = Chepy(ua).parse_user_agent()
{'user_agent': {'family': 'Other', 'major': None, 'minor': None, 'patch': None}, 'os': {'family': 'Other', 'major': None, 'minor': None, 'patch': None, 'patch_minor': None}, 'device': {'family': 'Other', 'brand': None, 'model': None}, 'string': 'ua'}
# The state type currently is Chepy
>>> c.o
# The state type now is a dict
>>> c.get("user_agent").get("family")
"Chrome"
# we are using the dict builtin method get to pull the values based on keys
This same behavior is replicated in the Chepy cli.
Cli only methods¶
For completeness sake everything is document here, but the only functions that are callable from the CLI are functions that start with cli_
.
Cli shell commands¶
It is possible to run shell commands from the cli py starting the command with a !
.
>>> !ls -la | grep py
This will run the following command inside the Chepy cli
- class chepy.modules.internal.cli.CliCompleter¶
- get_completions(document, complete_event)¶
This should be a generator that yields
Completion
instances.If the generation of completions is something expensive (that takes a lot of time), consider wrapping this Completer class in a ThreadedCompleter. In that case, the completer algorithm runs in a background thread and completions will be displayed as soon as they arrive.
- Parameters
document –
Document
instance.complete_event –
CompleteEvent
instance.
- chepy.modules.internal.cli.cli_delete_history()¶
Delete local history file
- chepy.modules.internal.cli.cli_exit(fire: object)¶
Exit the cli
- Parameters
fire (object) – The fire object
- chepy.modules.internal.cli.cli_get_attr(fire: object, attr: str)¶
Get attributes from current state type
- Parameters
fire (object) – The fire object
attr (str) – Required. A valid attr name
- chepy.modules.internal.cli.cli_get_state(fire: object, index: int)¶
Change the current state
- Parameters
fire (object) – The fire object
index (int) – Required. The index for the state
- chepy.modules.internal.cli.cli_go_back()¶
Go back one step
- chepy.modules.internal.cli.cli_highlight(fire: object, highlight: str)¶
Highlight regex match for cli
- Parameters
fire (object) – The fire object.
highlight (str) – Regex to highlight
- chepy.modules.internal.cli.cli_plugin_path(config)¶
Print the current plugin path
- chepy.modules.internal.cli.cli_pretty_print(fire: object)¶
Pretty print the current state
- Parameters
fire (object) – The fire object
- chepy.modules.internal.cli.cli_show_buffers(fire: object, pretty: bool = False)¶
Show all current buffers
- Parameters
fire (object) – The fire object
pretty (bool) – Pretty print output. Defaults to False
- chepy.modules.internal.cli.cli_show_dict_keys(fire: object, pretty: bool = False)¶
Get the dict keys of the current state.
- Parameters
fire (object) – The fire object
pretty (bool) – Pretty print output. Defaults to False
- chepy.modules.internal.cli.cli_show_errors(errors)¶
Show the errors messages if any
- chepy.modules.internal.cli.cli_show_length(fire: object)¶
Get the length of the current state.
- Parameters
fire (object) – The fire object
- chepy.modules.internal.cli.cli_show_states(fire: object, pretty: bool = False)¶
Change the current state
- Parameters
fire (object) – The fire object
pretty (bool) – Pretty print output. Defaults to False
- chepy.modules.internal.cli.cli_state_type(fire: object)¶
Get the current state type
- Parameters
fire (object) – The fire object
- chepy.modules.internal.cli.functions_cli()¶
Get all the function names from this module
- chepy.modules.internal.cli.get_doc(method: str)¶
Get docs for a method