Skip to content

Commit

Permalink
#576 split arg parsing (ArgParse) to new header
Browse files Browse the repository at this point in the history
- Now the parsing and configuration are separate
  types, even if still sharing the same cc file for declarations.

  The primary reason is to reduce header-bleed of implementation
  related types (such as vector<int,string>). This also
  force uniform access to ArgConfig for all..

  Also makes future non-static conversion of ArgParse
  much more.. lucrative. (eg. supply result message as
  member instead of returning outward)
  • Loading branch information
pnstickne authored and lifflander committed Jun 11, 2020
1 parent e328545 commit 1d6f4fc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
68 changes: 68 additions & 0 deletions src/vt/configs/arguments/argparse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
//@HEADER
// *****************************************************************************
//
// args.h
// DARMA Toolkit v. 1.0.0
// DARMA/vt => Virtual Transport
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact [email protected]
//
// *****************************************************************************
//@HEADER
*/

#if !defined INCLUDED_VT_CONFIGS_ARGUMENTS_ARGPARSE_H
#define INCLUDED_VT_CONFIGS_ARGUMENTS_ARGPARSE_H

#include <string>
#include <tuple>

namespace vt { namespace arguments {

struct ArgParse {

/// Parse the arguments into ArgConfig.
/// Re-assigns argc/argv to remove consumed arguments.
/// On success the tuple will be {-1, ""}. Otherwise the exit code
/// (which may be 0 if help was requested) will be returned along
/// with an appropriate display message.
static std::tuple<int, std::string> parse(int& argc, char**& argv);

private:
static bool parsed_;
};

}} /* end namespace vt::arguments */

#endif /*INCLUDED_VT_CONFIGS_ARGUMENTS_ARGPARSE_H*/
13 changes: 7 additions & 6 deletions src/vt/configs/arguments/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
*/

#include "vt/config.h"
#include "vt/configs/arguments/argparse.h"
#include "vt/configs/arguments/args.h"

#include <string>
Expand Down Expand Up @@ -177,7 +178,7 @@ namespace vt { namespace arguments {
/*static*/ std::vector<char*> ArgConfig::mpi_init_args;
/*static*/ std::vector<char*> ArgConfig::passthru_args;

/*static*/ bool ArgConfig::parsed_ = false;
/*static*/ bool ArgParse::parsed_ = false;

void addColorArgs(CLI::App& app) {
auto quiet = "Quiet the output from vt (only errors, warnings)";
Expand Down Expand Up @@ -559,7 +560,7 @@ void addSchedulerArgs(CLI::App& app) {

std::tuple<int, std::string> parseArguments(CLI::App& app, int& argc, char**& argv);

/*static*/ std::tuple<int, std::string> ArgConfig::parse(int& argc, char**& argv) {
/*static*/ std::tuple<int, std::string> ArgParse::parse(int& argc, char**& argv) {
if (parsed_ || argc == 0 || argv == nullptr) {
// Odd case.. pretend nothing bad happened.
return std::make_tuple(-1, std::string{});
Expand Down Expand Up @@ -588,12 +589,12 @@ std::tuple<int, std::string> parseArguments(CLI::App& app, int& argc, char**& ar
}

// Determine the final colorization setting.
if (vt_no_color) {
colorize_output = false;
if (ArgConfig::vt_no_color) {
ArgConfig::colorize_output = false;
} else {
// Otherwise, colorize.
// (Within MPI there is no good method to auto-detect.)
colorize_output = true;
ArgConfig::colorize_output = true;
}

parsed_ = true;
Expand Down Expand Up @@ -679,7 +680,7 @@ std::tuple<int, std::string> parseArguments(CLI::App& app, int& argc, char**& ar
}
new_argv[i++] = nullptr;

// Set them back with all vt arguments elided
// Set them back with all vt (and MPI) arguments elided
argc = new_argc;
argv = new_argv.get();

Expand Down
7 changes: 5 additions & 2 deletions src/vt/runtime/runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@
#include "vt/vrt/collection/balance/lb_type.h"
#include "vt/worker/worker_headers.h"
#include "vt/configs/debug/debug_colorize.h"
#include "vt/configs/arguments/args.h"
#include "vt/configs/error/stack_out.h"
#include "vt/configs/error/pretty_print_stack.h"
#include "vt/utils/memory/memory_usage.h"
#include "vt/runtime/component/component_pack.h"
#include "vt/utils/mpi_limits/mpi_max_tag.h"
#include "vt/vrt/collection/balance/stats_restart_reader.h"

#include "vt/configs/arguments/argparse.h"
#include "vt/configs/arguments/args.h"

#include <memory>
#include <iostream>
#include <functional>
Expand Down Expand Up @@ -107,7 +109,8 @@ Runtime::Runtime(

// n.b. ref-update of args with pass-through arguments
// (pass-through arguments are neither for VT or MPI_Init)
std::tuple<int, std::string> result = ArgType::parse(/*out*/ argc, /*out*/ argv);
std::tuple<int, std::string> result =
arguments::ArgParse::parse(/*out*/ argc, /*out*/ argv);
int exit_code = std::get<0>(result);

if (exit_code not_eq -1) {
Expand Down

0 comments on commit 1d6f4fc

Please sign in to comment.