TSDuck v3.40-3963
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::CommandLine Class Reference

A basic command line interpreter. More...

#include <tsCommandLine.h>

Inheritance diagram for ts::CommandLine:

Public Member Functions

 CommandLine (Report &report=(ts::CerrReport::Instance()))
 Constructor.
 
void addPredefinedCommands ()
 Add the predefined commands help, quit and exit.
 
bool analyzeCommand (const UString &command)
 Analyze a command line.
 
bool analyzeCommand (const UString &name, const UStringVector &arguments)
 Analyze a command line.
 
Argscommand (const UString &name, const UString &description=UString(), const UString &syntax=UString(), int flags=0)
 Add the definition of a command to the interpreter, without command handler.
 
template<class HANDLER , typename std::enable_if< std::is_base_of< CommandLineHandler, HANDLER >::value, int >::type = 0>
Argscommand (HANDLER *handler, CommandStatus(HANDLER::*method)(const UString &, Args &), const UString &name, const UString &description=UString(), const UString &syntax=UString(), int flags=0)
 Add the definition of a command to the interpreter.
 
UString getAllHelpText (Args::HelpFormat format, size_t line_width=79) const
 Get a formatted help text for all commands.
 
CommandStatus processCommand (const UString &line, Report *redirect=nullptr)
 Analyze and process a command line.
 
CommandStatus processCommand (const UString &name, const UStringVector &arguments, Report *redirect=nullptr)
 Analyze and process a command line.
 
CommandStatus processCommandFile (const UString &file_name, bool exit_on_error=false, Report *redirect=nullptr)
 Analyze and process all commands from a text file.
 
CommandStatus processCommandFiles (const UStringVector &file_names, bool exit_on_error=false, Report *redirect=nullptr)
 Analyze and process all commands from several text files.
 
CommandStatus processCommands (UStringVector &lines, bool exit_on_error=false, Report *redirect=nullptr)
 Analyze and process all commands from a vector of text lines.
 
CommandStatus processInteractive (bool exit_on_error, Report *redirect=nullptr)
 Analyze and process all commands from an interactive session.
 
CommandStatus processInteractive (const UString &prompt=EditLine::DefaultPrompt(), const UString &next_prompt=EditLine::DefaultNextPrompt(), const UString &history_file=EditLine::DefaultHistoryFile(), size_t history_size=EditLine::DefaultHistorySize(), bool exit_on_error=false, Report *redirect=nullptr)
 Analyze and process all commands from an interactive session.
 
bool processRedirections (bool on)
 Set command line redirection from files.
 
template<class HANDLER , typename std::enable_if< std::is_base_of< CommandLineHandler, HANDLER >::value, int >::type = 0>
void setCommandLineHandler (HANDLER *handler, CommandStatus(HANDLER::*method)(const UString &, Args &), const UString &name=UString())
 Set a new command line handler for one or all commands.
 
void setShell (const UString &shell)
 Set the "shell" string for all commands.
 

Detailed Description

A basic command line interpreter.

Constructor & Destructor Documentation

◆ CommandLine()

ts::CommandLine::CommandLine ( Report report = (ts::CerrReport::Instance()))

Constructor.

Parameters
[in,out]reportReference to a report where all messages are displayed. The reference must remain valid as long as this object exists.

Member Function Documentation

◆ setShell()

void ts::CommandLine::setShell ( const UString shell)

Set the "shell" string for all commands.

Parameters
[in]shellShell name string.
See also
Args::setShell()

◆ command() [1/2]

Args * ts::CommandLine::command ( const UString name,
const UString description = UString(),
const UString syntax = UString(),
int  flags = 0 
)
inline

Add the definition of a command to the interpreter, without command handler.

Parameters
[in]nameCommand name.
[in]descriptionA short one-line description of the command.
[in]syntaxA short one-line syntax summary, eg. "[options] filename ...".
[in]flagsAn or'ed mask of Args::Flags values.
Returns
A pointer to the Args for this command. The application will typically add options to this Args.
See also
Args::Flags
Args::Args()

◆ command() [2/2]

template<class HANDLER , typename std::enable_if< std::is_base_of< CommandLineHandler, HANDLER >::value, int >::type = 0>
Args * ts::CommandLine::command ( HANDLER *  handler,
CommandStatus(HANDLER::*)(const UString &, Args &)  method,
const UString name,
const UString description = UString(),
const UString syntax = UString(),
int  flags = 0 
)
inline

Add the definition of a command to the interpreter.

Template Parameters
HANDLERA subclass of CommandLineHandler.
Parameters
[in]handlerCommand handler object. A null pointer is allowed (unhandled command).
[in]methodCommand handler method. A null pointer is allowed (unhandled command).
[in]nameCommand name.
[in]descriptionA short one-line description of the command.
[in]syntaxA short one-line syntax summary, eg. "[options] filename ...".
[in]flagsAn or'ed mask of Args::Flags values.
Returns
A pointer to the Args for this command. The application will typically add options to this Args.
See also
Args::Flags
Args::Args()

◆ setCommandLineHandler()

template<class HANDLER , typename std::enable_if< std::is_base_of< CommandLineHandler, HANDLER >::value, int >::type = 0>
void ts::CommandLine::setCommandLineHandler ( HANDLER *  handler,
CommandStatus(HANDLER::*)(const UString &, Args &)  method,
const UString name = UString() 
)
inline

Set a new command line handler for one or all commands.

Template Parameters
HANDLERA subclass of CommandLineHandler.
Parameters
[in]handlerCommand handler object. A null pointer is allowed (unhandled command).
[in]methodCommand handler method. A null pointer is allowed (unhandled command).
[in]nameCommand name. If empty (the default), handler is set on all commands.

◆ addPredefinedCommands()

void ts::CommandLine::addPredefinedCommands ( )

Add the predefined commands help, quit and exit.

Useful for interactive interpreters.

◆ processRedirections()

bool ts::CommandLine::processRedirections ( bool  on)

Set command line redirection from files.

Parameters
[in]onIf true, process command line arguments redirection. All lines with the form '@filename' are replaced by the content The initial default is false.
Returns
The previous state of the redirections.

◆ analyzeCommand() [1/2]

bool ts::CommandLine::analyzeCommand ( const UString command)

Analyze a command line.

Parameters
[in]commandFull command line, with command name and parameters. Parameters are separated with spaces. Special characters and spaces must be escaped or quoted in the parameters.
Returns
True if the command is correct, false otherwise.

◆ analyzeCommand() [2/2]

bool ts::CommandLine::analyzeCommand ( const UString name,
const UStringVector arguments 
)

Analyze a command line.

Parameters
[in]nameCommand name.
[in]argumentsArguments from command line.
Returns
True if the command is correct, false otherwise.

◆ processCommand() [1/2]

CommandStatus ts::CommandLine::processCommand ( const UString line,
Report redirect = nullptr 
)

Analyze and process a command line.

Parameters
[in]lineFull command line, with command name and parameters. Parameters are separated with spaces. Special characters and spaces must be escaped or quoted in the parameters.
[in]redirectIf not null, temporarily redirect errors here.
Returns
Command status.

◆ processCommand() [2/2]

CommandStatus ts::CommandLine::processCommand ( const UString name,
const UStringVector arguments,
Report redirect = nullptr 
)

Analyze and process a command line.

Parameters
[in]nameCommand name.
[in]argumentsArguments from command line.
[in]redirectIf not null, temporarily redirect errors here.
Returns
Command status.

◆ processCommands()

CommandStatus ts::CommandLine::processCommands ( UStringVector lines,
bool  exit_on_error = false,
Report redirect = nullptr 
)

Analyze and process all commands from a vector of text lines.

Parameters
[in,out]linesLines of text. Lines starting with a '#' are considered as comments and removed. A line ending with a backslash means that the command continues on the next line. Such lines are reassembled.
[in]exit_on_errorIf true, exit command session on first error.
[in]redirectIf not null, temporarily redirect errors here.
Returns
Command status of the last command.

◆ processCommandFile()

CommandStatus ts::CommandLine::processCommandFile ( const UString file_name,
bool  exit_on_error = false,
Report redirect = nullptr 
)

Analyze and process all commands from a text file.

Lines starting with a '#' are considered as comments and ignored. A line ending with a backslash means that the command continues on the next line.

Parameters
[in]file_nameFile name. If empty or "-", execute an interactive session from the standard input.
[in]exit_on_errorIf true, exit command session on first error.
[in]redirectIf not null, temporarily redirect errors here.
Returns
Command status of the last command.

◆ processCommandFiles()

CommandStatus ts::CommandLine::processCommandFiles ( const UStringVector file_names,
bool  exit_on_error = false,
Report redirect = nullptr 
)

Analyze and process all commands from several text files.

Lines starting with a '#' are considered as comments and ignored. A line ending with a backslash means that the command continues on the next line.

Parameters
[in]file_namesVector of file name. If one file name is empty or "-", execute an interactive session from the standard input.
[in]exit_on_errorIf true, exit command session on first error.
[in]redirectIf not null, temporarily redirect errors here.
Returns
Command status of the last command.

◆ processInteractive() [1/2]

CommandStatus ts::CommandLine::processInteractive ( const UString prompt = EditLine::DefaultPrompt(),
const UString next_prompt = EditLine::DefaultNextPrompt(),
const UString history_file = EditLine::DefaultHistoryFile(),
size_t  history_size = EditLine::DefaultHistorySize(),
bool  exit_on_error = false,
Report redirect = nullptr 
)

Analyze and process all commands from an interactive session.

Parameters
[in]promptCommand line prompt.
[in]next_promptCommand line prompt for continuation lines.
[in]history_fileFile to load/save the history. If empty, no history is loaded or saved.
[in]history_sizeMaximum number of history lines to save.
[in]exit_on_errorIf true, exit command session on first error.
[in]redirectIf not null, temporarily redirect errors here.
Returns
Command status of the last command.
See also
EditLine

◆ processInteractive() [2/2]

CommandStatus ts::CommandLine::processInteractive ( bool  exit_on_error,
Report redirect = nullptr 
)

Analyze and process all commands from an interactive session.

Use all defaults for prompts and history.

Parameters
[in]exit_on_errorIf true, exit command session on first error.
[in]redirectIf not null, temporarily redirect errors here.
Returns
Command status of the last command.
See also
EditLine

◆ getAllHelpText()

UString ts::CommandLine::getAllHelpText ( Args::HelpFormat  format,
size_t  line_width = 79 
) const

Get a formatted help text for all commands.

Parameters
[in]formatRequested format of the help text.
[in]line_widthMaximum width of text lines.
Returns
The formatted help text.

The documentation for this class was generated from the following file: