From b0eeaf43273d58f263faab2755636de41d5f4ad5 Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 20 Aug 2015 21:28:56 -0300 Subject: [PATCH] Add alias() member to base::ProgramOptions::Option --- src/base/program_options.cpp | 19 ++++++++++++++++--- src/base/program_options.h | 5 ++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/base/program_options.cpp b/src/base/program_options.cpp index 22ed27be3..7fad4cf56 100644 --- a/src/base/program_options.cpp +++ b/src/base/program_options.cpp @@ -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); diff --git a/src/base/program_options.h b/src/base/program_options.h index a61c5465a..c72991c05 100644 --- a/src/base/program_options.h +++ b/src/base/program_options.h @@ -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.