cnfgen.clitools.cmdline module

Components for command line interface

CNFgen has many command line entry points to its functionality, and some of them expose the same functionality over and over. This module contains useful common components.

Copyright (C) 2012, 2013, 2014, 2015, 2016, 2019, 2020 Massimo Lauria <massimo.lauria@uniroma1.it> https://github.com/MassimoLauria/cnfgen.git

exception CLIError

Bases: Exception

Error related to the command line arguments

This error occurs when the command line contains some errors.

class CLIHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)

Bases: argparse.RawDescriptionHelpFormatter

Methods

format_help()
start_section(heading)
add_argument  
add_arguments  
add_text  
add_usage  
end_section  
class CLIParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True)

Bases: argparse.ArgumentParser

Argument Parser for CNFGen

Exactly as argparse.ArgumentParser, but the error function raises an exception, instead of calling exit.

Methods

add_argument(dest, …[, name, name])
add_subparsers(**kwargs)
error(message) Prints a usage message incorporating the message to stderr and exits.
exit([status, message])
format_usage()
parse_args([args, namespace])
print_usage([file])
register(registry_name, value, object)
set_defaults(**kwargs)
add_argument_group  
add_mutually_exclusive_group  
convert_arg_line_to_args  
format_help  
get_default  
parse_intermixed_args  
parse_known_args  
parse_known_intermixed_args  
print_help  
error(message: string)

Prints a usage message incorporating the message to stderr and exits.

If you override this in a subclass, it should not return – it should either exit or raise an exception.

compose_two_parsers(parser1, parser2, test=None)

Create an argparse action which compose two parsers

The action takes all remaining arguments and uses a test to determine which parser should parse them, using test function, given as argument.

If test is None then uses the default test:
is the first argument a number —> parser1 otherwise —> parser2
find_in_package(package, test, sortkey=None)

Explore a package for items that satisfy a speficic test

get_formula_helpers()
get_transformation_helpers()
nonnegative_int(value)
paginate_or_redirect_stdout(outputstream)

Output to a file or, when interactive, to the PAGER

Redirect standard output to outputstream. Furthermore when the standard output is supposed to go to an interactive terminal (i.e. it has not been piped to a file or to another process) then instead of flashing it on the screen this context manager redirects it to a temporary file which is shown by $PAGER, or by less if PAGER environment variable is not defined.

positive_even_int(value)
positive_int(value)
probability(value)
redirect_stdin(stream)

Redirect stdin during a test TODO: move to contextlib.redirect_stdin once it is implemented

setup_SIGINT()

Register a handler for SIGINT signal

Register a handler that manages keyboard interruptions via SIGINT.