iash  v0.5
Library to provide a bash-like shell in an application
UserCommand Class Reference

Used to retain command arguments and the proper input and output streams for a command call. More...

#include <UserCommand.h>

Public Member Functions

 UserCommand (const std::string &inputCommand, std::istream &in, std::ostream &out)
 Constructs a new UserCommand object with the given command, input, and output. More...
 
 UserCommand (const Token &inputCommand, std::istream &in, std::ostream &out)
 Constructs a new UserCommand object with the given command, input, and output. More...
 
const std::vector< std::string > & getWholeCommand () const
 Returns the entire command (including the command name), parsed into arguments as a vector. More...
 
std::vector< std::string > getArguments () const
 Returns all arguments passed by the user that are not options/switches (arguments identified with a - or -- at the beginning). More...
 
std::vector< std::string > getOptions () const
 Returns all of the options/flags that the user specified (arguments with a leading - or --). More...
 
bool hasOption (char opt) const
 Returns whether the given single-letter option was specified by the user. More...
 
bool hasOption (const std::string &opt) const
 Returns whether the given whole-word option was specified by the user. More...
 
std::string getContextualArgument (char opt) const
 Finds the argument following the specified single-letter option or flag. More...
 
std::string getContextualArgument (std::string opt) const
 Finds the argument following the specified multi-character option or flag. More...
 
std::istream & getStdin () const
 Gets a reference to the input stream to be used as stdin for this command invocation. More...
 
std::ostream & getStdout () const
 Gets a reference to the output stream to be used as stdout for this command invocation. More...
 

Detailed Description

Used to retain command arguments and the proper input and output streams for a command call.

Note that, with the exception of UserCommand::getWholeCommand, all of the functions that retrieve options or arguments from the user's command input iterate over the internal vector used to store it, and thus may slow down your program if they are called too often.

Since
0.5

Constructor & Destructor Documentation

◆ UserCommand() [1/2]

UserCommand::UserCommand ( const std::string &  inputCommand,
std::istream &  in,
std::ostream &  out 
)

Constructs a new UserCommand object with the given command, input, and output.

This also invokes the command parser, parsing the user's command into its components. (This does not parse out the command's options).

Parameters
inputCommandthe command the user input
inthe stream to use as stdin for the command
outthe stream to use as out for the command

◆ UserCommand() [2/2]

UserCommand::UserCommand ( const Token inputCommand,
std::istream &  in,
std::ostream &  out 
)

Constructs a new UserCommand object with the given command, input, and output.

This also invokes the command parser, parsing the user's command into its components. (This does not parse out the command's options).

Parameters
inputCommandthe command the user input as a Token
inthe stream to use as stdin for the command
outthe stream to use as stdout for the command

Member Function Documentation

◆ getArguments()

vector< string > UserCommand::getArguments ( ) const

Returns all arguments passed by the user that are not options/switches (arguments identified with a - or -- at the beginning).

Returns
all non-option arguments

◆ getContextualArgument() [1/2]

string UserCommand::getContextualArgument ( char  opt) const

Finds the argument following the specified single-letter option or flag.

If the specified option does not have an argument that follows it, or the option is embedded in a chained single-letter argument and is not at the end (for example, c in -abcde), then this will return an empty string.

Note that this does not guarantee that the returned argument is actually specifying a parameter for the given option. In order to insure this, Command::validate should be overridden to check that the command structure is valid.

Parameters
optthe single-letter option to find a contextual argument for
Returns
the argument following the option, or a blank string if one could not be found

◆ getContextualArgument() [2/2]

std::string UserCommand::getContextualArgument ( std::string  opt) const

Finds the argument following the specified multi-character option or flag.

If the specified option dooes not have an argument that follows it, then this will return an empty string.

Note that this does not guarantee that the returned argument is actually specifying a parameter for the given option. In order to insure this, Command::validate should be overridden to check that the command structure is valid.

Parameters
opta multi-character option to find a contextual argument for
Returns
the argument following the option, or a blank string if one could not be found

◆ getOptions()

vector< string > UserCommand::getOptions ( ) const

Returns all of the options/flags that the user specified (arguments with a leading - or --).

Note that iash option syntax allows users to chain single-letter options together (like -abcde), and will report all of the characters present after a single minus as individual options (so, in this example, a, b, c, d, e will all be reported as options). If you want iash to report whole words as options, have users enter them with a double-minus prefix (so --abcde will be reported as abcde).

Returns
all options/flags

◆ getStdin()

istream & UserCommand::getStdin ( ) const

Gets a reference to the input stream to be used as stdin for this command invocation.

Returns
a reference to the input stream for this command

◆ getStdout()

ostream & UserCommand::getStdout ( ) const

Gets a reference to the output stream to be used as stdout for this command invocation.

Returns
a reference to the output stream for this command

◆ getWholeCommand()

const vector< string > & UserCommand::getWholeCommand ( ) const

Returns the entire command (including the command name), parsed into arguments as a vector.

Returns
the entire command, including the command name, as a vector

◆ hasOption() [1/2]

bool UserCommand::hasOption ( char  opt) const

Returns whether the given single-letter option was specified by the user.

Note that iash option syntax allows users to chain single-letter options together (like -abcde), and will report all of the characters present after a single minus as individual options (so, in this example, a, b, c, d, e will all be reported as options). If you want iash to report whole words as options, have users enter them with a double-minus prefix (so --abcde will be reported as abcde).

@param opt   the single-letter option to check for
@return      whether the option was passed by the user

◆ hasOption() [2/2]

bool UserCommand::hasOption ( const std::string &  opt) const

Returns whether the given whole-word option was specified by the user.

Note that iash option syntax allows users to chain single-letter options together (like -abcde), and will report all of the characters present after a single minus as individual options (so, in this example, a, b, c, d, e will all be reported as options). If you want iash to report whole words as options, have users enter them with a double-minus prefix (so --abcde will be reported as abcde).

@param opt   the whole-word option to check for
@return      whether the option was passed by the user

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