1 #ifndef _command__hpp__included__
2 #define _command__hpp__included__
16 * Register a new command.
18 * parameter cmd: The command to register.
19 * throws std::bad_alloc: Not enough memory.
21 command(const std::string
& cmd
) throw(std::bad_alloc
);
24 * Deregister a command.
26 virtual ~command() throw();
31 * parameter arguments: Arguments to command.
32 * throws std::bad_alloc: Not enough memory.
33 * throws std::runtime_error: Command execution failed.
35 virtual void invoke(const std::string
& arguments
) throw(std::bad_alloc
, std::runtime_error
) = 0;
38 * Look up and invoke a command. The command will undergo alias expansion and recursion checking.
40 * parameter cmd: Command to exeucte.
42 static void invokeC(const std::string
& cmd
) throw();
46 static std::set
<std::string
> get_aliases() throw(std::bad_alloc
);
50 static std::string
get_alias_for(const std::string
& aname
) throw(std::bad_alloc
);
54 static void set_alias_for(const std::string
& aname
, const std::string
& avalue
) throw(std::bad_alloc
);
56 * Is alias name valid.
58 static bool valid_alias_name(const std::string
& aname
) throw(std::bad_alloc
);
60 * Get short help for command.
62 virtual std::string
get_short_help() throw(std::bad_alloc
);
65 * Get long help for command.
67 virtual std::string
get_long_help() throw(std::bad_alloc
);
69 command(const command
&);
70 command
& operator=(const command
&);
71 std::string commandname
;
80 * The filename itself.
84 * Return the filename.
86 * returns: The filename.
88 operator std::string() { return v
; }
92 * Run command function helper.
94 * parameter fn: Function pointer to invoke.
95 * parameter a: The arguments to pass.
97 template<typename
... args
>
98 void invoke_command_fn(void (*fn
)(args
... arguments
), const std::string
& a
);
101 * Warp function pointer as command.
103 template<typename
... args
>
104 class function_ptr_command
: public command
108 * Create a new command.
110 * parameter name: Name of the command
111 * parameter description Description for the command
112 * parameter help: Help for the command.
113 * parameter fn: Function to call on command.
115 function_ptr_command(const std::string
& name
, const std::string
& _description
, const std::string
& _help
,
116 void (*_fn
)(args
... arguments
)) throw(std::bad_alloc
)
119 description
= _description
;
126 ~function_ptr_command() throw()
132 * parameter a: Arguments to function.
134 void invoke(const std::string
& a
) throw(std::bad_alloc
, std::runtime_error
)
136 invoke_command_fn(fn
, a
);
139 * Get short description.
141 * returns: Description.
142 * throw std::bad_alloc: Not enough memory.
144 std::string
get_short_help() throw(std::bad_alloc
)
152 * throw std::bad_alloc: Not enough memory.
154 std::string
get_long_help() throw(std::bad_alloc
)
159 void (*fn
)(args
... arguments
);
160 std::string description
;