diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-05-28 23:50:11 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-05-28 23:50:11 +0000 |
| commit | 1257d432b4f08cdfecf74a259b596938cce76a1e (patch) | |
| tree | 93f7ac41fc4d77cbc180237103c0cef1a04247cc /src | |
| parent | d96427b83186835107bc4331f91a9f8924d8bb3f (diff) | |
the 'menu' commands now takes an optional -auto-single option
-auto-single tell the 'menu' command not to prompt when there
is only a single choice, and to automatically execute it's
command.
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands.cc | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/src/commands.cc b/src/commands.cc index 7e59b4c7..93b3fdab 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -188,6 +188,19 @@ struct ParametersParser return iterator(*this, m_params.size()); } + // access positional parameter by index + const String& operator[] (size_t index) const + { + assert(index < positional_count()); + iterator it = begin(); + while (index) + { + ++it; + --index; + } + return *it; + } + private: const CommandParameters& m_params; std::vector<bool> m_positional; @@ -674,21 +687,30 @@ void eval_string(const CommandParameters& params, void menu(const CommandParameters& params, const Context& context) { - if (params.size() == 0 or (params.size() % 2) != 0) + ParametersParser parser(params, { { "auto-single", false } }); + + size_t count = parser.positional_count(); + if (count == 0 or (count % 2) != 0) throw wrong_argument_count(); + if (count == 2 and parser.has_option("auto-single")) + { + CommandManager::instance().execute(parser[1], context); + return; + } + std::ostringstream oss; - for (int i = 0; i < params.size(); i += 2) + for (int i = 0; i < count; i += 2) { - oss << i/2 + 1 << "[" << params[i] << "] "; + oss << i/2 + 1 << "[" << parser[i] << "] "; } oss << "(empty cancels): "; String choice = prompt_func(oss.str(), complete_nothing); int i = atoi(choice.c_str()); - if (i > 0 and i < (params.size() / 2) + 1) - CommandManager::instance().execute(params[(i-1)*2+1], context); + if (i > 0 and i < (count / 2) + 1) + CommandManager::instance().execute(parser[(i-1)*2+1], context); } } |
