Add alias() member to base::ProgramOptions::Option

This commit is contained in:
David Capello 2015-08-20 21:28:56 -03:00
parent 3326c94aec
commit b0eeaf4327
2 changed files with 20 additions and 4 deletions

View File

@ -1,5 +1,5 @@
// Aseprite Base Library
// Copyright (c) 2001-2013 David Capello
// Copyright (c) 2001-2013, 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -22,7 +22,8 @@ struct same_name {
const string& name;
same_name(const string& name) : name(name) { }
bool operator()(const ProgramOptions::Option* a) {
return a->name() == name;
return (a->name() == name ||
a->alias() == name);
}
};
@ -192,7 +193,7 @@ std::ostream& operator<<(std::ostream& os, const base::ProgramOptions& po)
it=po.options().begin(), end=po.options().end(); it != end; ++it) {
const base::ProgramOptions::Option* option = *it;
std::size_t optionWidth =
6+option->name().size()+1+
6+MAX(option->name().size(), option->alias().size())+1+
(option->doesRequireValue() ? option->getValueName().size()+1: 0);
if (maxOptionWidth < optionWidth)
@ -213,6 +214,18 @@ std::ostream& operator<<(std::ostream& os, const base::ProgramOptions& po)
if (option->doesRequireValue())
os << " " << option->getValueName();
// Show alias
if (!option->alias().empty()) {
os << " or\n"
<< std::setw(6) << ' '
<< "--" << option->alias();
if (option->doesRequireValue())
os << " " << option->getValueName();
optionWidth = 6+option->alias().size()+1+
(option->doesRequireValue() ? option->getValueName().size()+1: 0);
}
if (!option->description().empty()) {
bool multilines = (option->description().find('\n') != std::string::npos);

View File

@ -1,5 +1,5 @@
// Aseprite Base Library
// Copyright (c) 2001-2013 David Capello
// Copyright (c) 2001-2013, 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -43,11 +43,13 @@ namespace base {
}
// Getters
const std::string& name() const { return m_name; }
const std::string& alias() const { return m_alias; }
const std::string& description() const { return m_description; }
const std::string& getValueName() const { return m_valueName; }
char mnemonic() const { return m_mnemonic; }
bool doesRequireValue() const { return !m_valueName.empty(); }
// Setters
Option& alias(const std::string& alias) { m_alias = alias; return *this; }
Option& description(const std::string& desc) { m_description = desc; return *this; }
Option& mnemonic(char mnemonic) { m_mnemonic = mnemonic; return *this; }
Option& requiresValue(const std::string& valueName) {
@ -56,6 +58,7 @@ namespace base {
}
private:
std::string m_name; // Name of the option (e.g. "help" for "--help")
std::string m_alias;
std::string m_description; // Description of the option (this can be used when the help is printed).
std::string m_valueName; // Empty if this option doesn't require a value, or the name of the expected value.
char m_mnemonic; // One character that can be used in the command line to use this option.