// Copyright (c) 2017-2022, University of Cincinnati, developed by Henry Schreiner // under NSF AWARD 1414736 and by the respective contributors. // All rights reserved. // // SPDX-License-Identifier: BSD-3-Clause #pragma once // [CLI11:public_includes:set] #include #include #include #include #include #include #include #include #include #include #include #include // [CLI11:public_includes:end] // CLI Library includes #include "ConfigFwd.hpp" #include "Error.hpp" #include "FormatterFwd.hpp" #include "Macros.hpp" #include "Option.hpp" #include "Split.hpp" #include "StringTools.hpp" #include "TypeTools.hpp" namespace CLI { // [CLI11:app_hpp:verbatim] #ifndef CLI11_PARSE #define CLI11_PARSE(app, argc, argv) \ try { \ (app).parse((argc), (argv)); \ } catch(const CLI::ParseError &e) { \ return (app).exit(e); \ } #endif namespace detail { enum class Classifier { NONE, POSITIONAL_MARK, SHORT, LONG, WINDOWS_STYLE, SUBCOMMAND, SUBCOMMAND_TERMINATOR }; struct AppFriend; } // namespace detail namespace FailureMessage { std::string simple(const App *app, const Error &e); std::string help(const App *app, const Error &e); } // namespace FailureMessage /// enumeration of modes of how to deal with extras in config files enum class config_extras_mode : char { error = 0, ignore, ignore_all, capture }; class App; using App_p = std::shared_ptr; namespace detail { /// helper functions for adding in appropriate flag modifiers for add_flag template ::value || (sizeof(T) <= 1U), detail::enabler> = detail::dummy> Option *default_flag_modifiers(Option *opt) { return opt->always_capture_default(); } /// summing modifiers template ::value && (sizeof(T) > 1U), detail::enabler> = detail::dummy> Option *default_flag_modifiers(Option *opt) { return opt->multi_option_policy(MultiOptionPolicy::Sum)->default_str("0")->force_callback(); } } // namespace detail class Option_group; /// Creates a command line program, with very few defaults. /** To use, create a new `Program()` instance with `argc`, `argv`, and a help description. The templated * add_option methods make it easy to prepare options. Remember to call `.start` before starting your * program, so that the options can be evaluated and the help option doesn't accidentally run your program. */ class App { friend Option; friend detail::AppFriend; protected: // This library follows the Google style guide for member names ending in underscores /// @name Basics ///@{ /// Subcommand name or program name (from parser if name is empty) std::string name_{}; /// Description of the current program/subcommand std::string description_{}; /// If true, allow extra arguments (ie, don't throw an error). INHERITABLE bool allow_extras_{false}; /// If ignore, allow extra arguments in the ini file (ie, don't throw an error). INHERITABLE /// if error error on an extra argument, and if capture feed it to the app config_extras_mode allow_config_extras_{config_extras_mode::ignore}; /// If true, return immediately on an unrecognized option (implies allow_extras) INHERITABLE bool prefix_command_{false}; /// If set to true the name was automatically generated from the command line vs a user set name bool has_automatic_name_{false}; /// If set to true the subcommand is required to be processed and used, ignored for main app bool required_{false}; /// If set to true the subcommand is disabled and cannot be used, ignored for main app bool disabled_{false}; /// Flag indicating that the pre_parse_callback has been triggered bool pre_parse_called_{false}; /// Flag indicating that the callback for the subcommand should be executed immediately on parse completion which is /// before help or ini files are processed. INHERITABLE bool immediate_callback_{false}; /// This is a function that runs prior to the start of parsing std::function pre_parse_callback_{}; /// This is a function that runs when parsing has finished. std::function parse_complete_callback_{}; /// This is a function that runs when all processing has completed std::function final_callback_{}; ///@} /// @name Options ///@{ /// The default values for options, customizable and changeable INHERITABLE OptionDefaults option_defaults_{}; /// The list of options, stored locally std::vector options_{}; ///@} /// @name Help ///@{ /// Footer to put after all options in the help output INHERITABLE std::string footer_{}; /// This is a function that generates a footer to put after all other options in help output std::function footer_callback_{}; /// A pointer to the help flag if there is one INHERITABLE Option *help_ptr_{nullptr}; /// A pointer to the help all flag if there is one INHERITABLE Option *help_all_ptr_{nullptr}; /// A pointer to a version flag if there is one Option *version_ptr_{nullptr}; /// This is the formatter for help printing. Default provided. INHERITABLE (same pointer) std::shared_ptr formatter_{new Formatter()}; /// The error message printing function INHERITABLE std::function failure_message_{FailureMessage::simple}; ///@} /// @name Parsing ///@{ using missing_t = std::vector>; /// Pair of classifier, string for missing options. (extra detail is removed on returning from parse) /// /// This is faster and cleaner than storing just a list of strings and reparsing. This may contain the -- separator. missing_t missing_{}; /// This is a list of pointers to options with the original parse order std::vector