1 #ifndef _command__hpp__included__
2 #define _command__hpp__included__
15 * Register a new command.
17 * parameter cmd: The command to register.
18 * throws std::bad_alloc: Not enough memory.
20 command(const std::string
& cmd
) throw(std::bad_alloc
);
23 * Deregister a command.
25 virtual ~command() throw();
30 * parameter arguments: Arguments to command.
31 * throws std::bad_alloc: Not enough memory.
32 * throws std::runtime_error: Command execution failed.
34 virtual void invoke(const std::string
& arguments
) throw(std::bad_alloc
, std::runtime_error
) = 0;
37 * Look up and invoke a command. The command will undergo alias expansion and recursion checking.
39 * parameter cmd: Command to exeucte.
41 static void invokeC(const std::string
& cmd
) throw();
44 * Get short help for command.
46 virtual std::string
get_short_help() throw(std::bad_alloc
);
49 * Get long help for command.
51 virtual std::string
get_long_help() throw(std::bad_alloc
);
53 command(const command
&);
54 command
& operator=(const command
&);
55 std::string commandname
;
59 * Splits string to fields on ' ' and '\t', with multiple whitespace collapsed into one.
65 * Create a new splitter.
67 * parameter _line: The line to start splitting.
68 * throws std::bad_alloc: Not enough memory.
70 tokensplitter(const std::string
& _line
) throw(std::bad_alloc
);
72 * Is there more coming?
74 * returns: True if there is more, false otherwise.
75 * throws std::bad_alloc: Not enough memory.
77 operator bool() throw();
81 * returns: The next token from line. If there is no more tokens, returns "".
82 * throws std::bad_alloc: Not enough memory.
84 operator std::string() throw(std::bad_alloc
);
86 * Get all that is remaining.
88 * returns: All remaining parts of line as-is.
89 * throws std::bad_alloc: Not enough memory.
91 std::string
tail() throw(std::bad_alloc
);
103 operator std::string() { return v
; }
107 * Run command function.
109 template<typename
... args
>
110 void invoke_command_fn(void (*fn
)(args
... arguments
), const std::string
& a
);
113 * Warp function pointer as command.
115 template<typename
... args
>
116 class function_ptr_command
: public command
120 * Create a new command.
122 * parameter name: Name of the command
123 * parameter description Description for the command
124 * parameter help: Help for the command.
125 * parameter fn: Function to call on command.
127 function_ptr_command(const std::string
& name
, const std::string
& _description
, const std::string
& _help
,
128 void (*_fn
)(args
... arguments
)) throw(std::bad_alloc
)
131 description
= _description
;
138 ~function_ptr_command() throw()
144 * parameter a: Arguments to function.
146 void invoke(const std::string
& a
) throw(std::bad_alloc
, std::runtime_error
)
148 invoke_command_fn(fn
, a
);
151 * Get short description.
153 * returns: Description.
154 * throw std::bad_alloc: Not enough memory.
156 std::string
get_short_help() throw(std::bad_alloc
)
164 * throw std::bad_alloc: Not enough memory.
166 std::string
get_long_help() throw(std::bad_alloc
)
171 void (*fn
)(args
... arguments
);
172 std::string description
;