From 78e3f55368cd7ca5e3ca291f18990501eac9e1ff Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 12 Aug 2016 15:53:48 -0700 Subject: 3170 - multiple --options at the commandline The mu commandline now has four parts: options, commands (of which we only have one so far: 'test'), files/directories and ingredients to pass to 'main'. That cleans up the hacky ordering constraint we had earlier. I've also cleaned up the usage message. --- 001help.cc | 66 ++++++++++++++++++++++++++++++++++++---------------------- 050scenario.cc | 9 +++++++- mu | 6 ++++-- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/001help.cc b/001help.cc index fa5c3547..ea76db25 100644 --- a/001help.cc +++ b/001help.cc @@ -5,28 +5,52 @@ if (argc <= 1 || is_equal(argv[1], "--help")) { // this is the functionality later layers will provide // currently no automated tests for commandline arg parsing - cerr << "To load files and run 'main':\n" - << " mu file1.mu file2.mu ...\n" - << "To run all tests:\n" - << " mu test\n" - << "To load files and then run all tests:\n" - << " mu test file1.mu file2.mu ...\n" - << "To load files and run only the tests in explicitly loaded files (for apps):\n" - << " mu --test-only-app test file1.mu file2.mu ...\n" - << "'--test-only-app' must come before all other args\n" - << "'test' must come before all other args except '--test-only-app'\n" - << "To load all files with a numeric prefix in a directory:\n" - << " mu directory1\n" - << "You can test directories just like files.\n" - << "To pass ingredients to a mu program, provide them after '--':\n" - << " mu file_or_dir1 file_or_dir2 ... -- ingredient1 ingredient2 ...\n" + if (argc <= 1) { + cerr << "Please provide a Mu program to run.\n" + << "\n"; + } + cerr << "Usage:\n" + << " mu [options] [test] [files]\n" + << "or:\n" + << " mu [options] [test] [files] -- [ingredients for function/recipe 'main']\n" + << "Square brackets surround optional arguments.\n" + << "\n" + << "Examples:\n" + << " To load files and run 'main':\n" + << " mu file1.mu file2.mu ...\n" + << " To run all tests:\n" + << " mu test\n" + << " To load files and then run all tests:\n" + << " mu test file1.mu file2.mu ...\n" + << " To load files and run only the tests in explicitly loaded files (for apps):\n" + << " mu --test-only-app test file1.mu file2.mu ...\n" + << " To load all files with a numeric prefix in a directory:\n" + << " mu directory1 directory2 ...\n" + << " You can test directories just like files.\n" + << " mu test directory1 directory2 ...\n" + << " To pass ingredients to a mu program, provide them after '--':\n" + << " mu file_or_dir1 file_or_dir2 ... -- ingredient1 ingredient2 ...\n" << "\n" - << "To browse a trace generated by a previous run:\n" - << " mu browse-trace file\n" + << " To browse a trace generated by a previous run:\n" + << " mu browse-trace file\n" ; return 0; } +//: Support for option parsing. +//: Options always begin with '--' and are always the first arguments. An +//: option will never follow a non-option. +:(before "End Commandline Parsing") +char** arg = &argv[1]; +while (argc > 1 && starts_with(*arg, "--")) { + if (false) + ; // no-op branch just so any further additions can consistently always start with 'else' + // End Commandline Options(*arg) + else + cerr << "skipping unknown option " << *arg << '\n'; + --argc; ++argv; ++arg; +} + //:: Helper function used by the above fragment of code (and later layers too, //:: who knows?). //: The :(code) directive appends function definitions to the end of the @@ -198,14 +222,6 @@ bool has_data(istream& in) { return in && !in.eof(); } -////: A hack to support faster debugging. -:(before "End Globals") -bool Test_only_app = false; -:(before "End Commandline Parsing") -if (argc > 1 && is_equal(argv[1], "--test-only-app")) { - Test_only_app = true; --argc; ++argv; -} - :(before "End Includes") #include diff --git a/050scenario.cc b/050scenario.cc index d14bbcbe..b814a2de 100644 --- a/050scenario.cc +++ b/050scenario.cc @@ -132,7 +132,14 @@ if (Num_core_mu_scenarios != SIZE(Scenarios)) { } } -//: Support running tests for just the Mu app(s) we are loading. +//: For faster debugging, support running tests for just the Mu app(s) we are +//: loading. +:(before "End Globals") +bool Test_only_app = false; +:(before "End Commandline Options(*arg)") +else if (is_equal(*arg, "--test-only-app")) { + Test_only_app = true; +} :(after "End Test Run Initialization") if (Test_only_app && Num_core_mu_scenarios < SIZE(Scenarios)) { goto run_app_scenarios; diff --git a/mu b/mu index 0a8dd082..71827c49 100755 --- a/mu +++ b/mu @@ -5,8 +5,10 @@ # show make output only if something needs doing make -q || make >&2 || exit 1 -# If I'm setting flags at the commandline I'm often disabling optimizations. -# In that case don't run all tests if I load any app files. +# Little bit of cleverness: If I'm setting flags at the commandline I'm often +# disabling optimizations. In that case don't run all tests if I load any app +# files. +# Might be too clever.. if [[ $CFLAGS && $# -gt 0 && $1 != '--help' ]] # latter two conditions are to continue printing the help message then ./mu_bin --test-only-app "$@" -- cgit 1.4.1-2-gfad0