typedargs.shell module

Hierarchical Shell provides as REPL like environment for executing commmands.

Summary

Classes:

HierarchicalShell A hierarchical shell for navigating through python package API functions.
InitialContext A basic context for holding the root callable functions for a shell.

Reference

class typedargs.shell.InitialContext[source]

Bases: dict

A basic context for holding the root callable functions for a shell.

context = True
decorated = False
finalizer = False
metadata = <typedargs.metadata.AnnotatedMetadata object>
takes_cmdline = False
class typedargs.shell.HierarchicalShell(name)[source]

Bases: object

A hierarchical shell for navigating through python package API functions.

add_builtin(name, callable)[source]

Add a builtin function callable from all contexts.

Callable should be an annotated function like any other that you want to call from a HierarchicalShell

Parameters:
  • name (str) – The name of the function
  • callable (callable) – The annotated function that we should call
root_update(dict_like)[source]

Add entries to root from a dict_line object.

root_add(name, value)[source]

Add a single function to the root context.

Parameters:
  • name (str) – The name of the callable to add
  • value (str or callable) – The callable function or a string to lazily resolve to the callable later.
context_name()[source]

Get the string name of the current context.

finished()[source]

Check if we have finalized all contexts.

Returns:True if there are no nested contexts left, False otherwise
Return type:bool
valid_identifiers()[source]

Get a list of all valid identifiers for the current context.

Returns:A list of all of the valid identifiers for this context
Return type:list(str)
find_function(context, funname)[source]

Find a function in the given context by name.

This function will first search the list of builtins and if the desired function is not a builtin, it will continue to search the given context.

Parameters:
  • context (object) – A dict or class that is a typedargs context
  • funname (str) – The name of the function to find
Returns:

The found function.

Return type:

callable

list_dir(context)[source]

Return a listing of all of the functions in this context including builtins.

Parameters:context (object) – The context to print a directory for.
Returns:str
process_arguments(func, args)[source]

Process arguments from the command line into positional and kw args.

Arguments are consumed until the argument spec for the function is filled or a – is found or there are no more arguments. Keyword arguments can be specified using –field=value, -f value or –field value. Positional arguments are specified just on the command line itself.

If a keyword argument (field) is a boolean, it can be set to True by just passing –field or -f without needing to explicitly pass True unless this would cause ambiguity in parsing since the next expected positional argument is also a boolean or a string.

Parameters:
  • func (callable) – A function previously annotated with type information
  • args (list) – A list of all of the potential arguments to this function.
Returns:

A tuple with a list of args, a dict of

keyword args and a list of any unused args that were not processed.

Return type:

(args, kw_args, unused args)

invoke_one(line)[source]

Invoke a function given a list of arguments with the function listed first.

The function is searched for using the current context on the context stack and its annotated type information is used to convert all of the string parameters passed in line to appropriate python types.

Parameters:line (list) – The list of command line arguments.
Returns:
A tuple containing the return value of the function, if any,
a boolean specifying if the function created a new context (False if a new context was created) and a list with the remainder of the command line if this function did not consume all arguments.
Return type:(object, list, bool)
invoke(line)[source]

Invoke a one or more function given a list of arguments.

The functions are searched for using the current context on the context stack and its annotated type information is used to convert all of the string parameters passed in line to appropriate python types.

Parameters:line (list) – The list of command line arguments.
Returns:
A boolean specifying if the last function created a new context
(False if a new context was created) and a list with the remainder of the command line if this function did not consume all arguments.)
Return type:bool
invoke_string(line)[source]

Parse and invoke a string line.

Parameters:line (str) – The line that we want to parse and invoke.
Returns:
A boolean specifying if the last function created a new context
(False if a new context was created) and a list with the remainder of the command line if this function did not consume all arguments.)
Return type:bool