From 8170b77be8c1ec66f4e860fe88dafa791031e57f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 7 May 2013 16:53:08 +0300 Subject: [PATCH] Use GetOpt's new features --- source/buildercli.cpp | 59 ++++++++++++++++--------------------------- source/buildercli.h | 6 ++--- 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/source/buildercli.cpp b/source/buildercli.cpp index bcd439e..0bdf877 100644 --- a/source/buildercli.cpp +++ b/source/buildercli.cpp @@ -63,10 +63,17 @@ BuilderCLI::BuilderCLI(int argc, char **argv): getopt.add_option( "max-depth", max_depth, GetOpt::REQUIRED_ARG).set_help("Show up to NUM levels in analysis.", "NUM"); getopt.add_option( "prefix", prefix, GetOpt::REQUIRED_ARG).set_help("Install things to DIR.", "DIR"); getopt.add_option( "tempdir", tempdir, GetOpt::REQUIRED_ARG).set_help("Store temporary files in DIR.", "DIR"); - usagemsg = getopt.generate_usage(argv[0])+" [ ...]"; - helpmsg = getopt.generate_help(); + getopt.add_argument("target", cmdline_targets, GetOpt::OPTIONAL_ARG).set_help("Target(s) to build"); getopt(argc, argv); + if(help) + { + helpmsg = "Usage:\n "; + helpmsg += getopt.generate_usage(argv[0], true); + helpmsg += "\n\n"; + helpmsg += getopt.generate_help(); + } + if(silent) --verbose; if(verbose>=1) @@ -166,26 +173,20 @@ BuilderCLI::~BuilderCLI() int BuilderCLI::main() { FS::Path main_file = cwd/build_file; - if(!FS::exists(main_file)) + if(FS::exists(main_file)) + builder.load_build_file(main_file, &cmdline_options, conf_all); + else if(!help) { - if(help) - { - usage(0, "builder", false); - return 0; - } - else - { - IO::print(IO::cerr, "The file %s does not exist.\n", main_file); - return 1; - } + IO::print(IO::cerr, "The file %s does not exist.\n", main_file); + return 1; } - builder.load_build_file(main_file, &cmdline_options, conf_all); - if(help) { - usage(0, "builder", false); - IO::print("\n"); + IO::print("Builder 1.0\n" + "Copyright © 2006-2013 Mikkosoft Productions, Mikko Rasa\n" + "Licensed under the GPL\n\n" + "%s", helpmsg); package_help(); return 0; } @@ -326,30 +327,17 @@ bool BuilderCLI::prepare_build() return true; } -void BuilderCLI::usage(const char *reason, const char *argv0, bool brief) -{ - if(reason) - IO::print(IO::cerr, "%s\n", reason); - - if(brief) - IO::print(IO::cerr, "Usage: %s\n", usagemsg); - else - { - IO::print(IO::cerr, "Builder 1.0\n\n"); - IO::print(IO::cerr, "Usage: %s [options] [ ...]\n\n", argv0); - IO::print(IO::cerr, "Options:\n"); - IO::print(IO::cerr, helpmsg); - } -} - void BuilderCLI::package_help() { PackageManager &package_manager = builder.get_package_manager(); + if(package_manager.get_packages().empty()) + return; + SourcePackage &main_pkg = dynamic_cast(package_manager.get_main_package()); const Config &config = main_pkg.get_config(); const Config::OptionMap &options = config.get_options(); - IO::print("Required packages:\n "); + IO::print("\nRequired packages:\n "); const Package::Requirements &requires = main_pkg.get_required_packages(); for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i) { @@ -367,6 +355,3 @@ void BuilderCLI::package_help() IO::print("\n"); } } - -string BuilderCLI::usagemsg; -string BuilderCLI::helpmsg; diff --git a/source/buildercli.h b/source/buildercli.h index ac88752..42ed0c3 100644 --- a/source/buildercli.h +++ b/source/buildercli.h @@ -22,6 +22,7 @@ private: unsigned clean; bool dry_run; bool help; + std::string helpmsg; bool show_progress; std::string build_file; unsigned jobs; @@ -31,18 +32,15 @@ private: bool build_all; bool create_makefile; - static std::string usagemsg; - static std::string helpmsg; - public: BuilderCLI(int, char **); ~BuilderCLI(); virtual int main(); +private: bool prepare_build(); - static void usage(const char *, const char *, bool); void package_help(); }; -- 2.45.2