Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Control/Walker/CmdLine/Parser.cpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.
7 : : All rights reserved. See the LICENSE file for details.
8 : : \brief Walker's command line parser
9 : : \details Walker's command line parser
10 : : */
11 : : // *****************************************************************************
12 : :
13 : : #include "NoWarning/pegtl.hpp"
14 : : #include "NoWarning/charm.hpp"
15 : :
16 : : #include "Print.hpp"
17 : : #include "QuinoaConfig.hpp"
18 : : #include "Exception.hpp"
19 : : #include "Keywords.hpp"
20 : : #include "Walker/Types.hpp"
21 : : #include "Walker/CmdLine/Parser.hpp"
22 : : #include "Walker/CmdLine/Grammar.hpp"
23 : : #include "Walker/CmdLine/CmdLine.hpp"
24 : : #include "Walker/InputDeck/InputDeck.hpp"
25 : :
26 : : namespace tk {
27 : : namespace grm {
28 : :
29 : : tk::Print g_print;
30 : :
31 : : } // grm::
32 : : } // tk::
33 : :
34 : : namespace walker {
35 : :
36 : : extern ctr::InputDeck g_inputdeck;
37 : :
38 : : } // walker::
39 : :
40 : : using walker::CmdLineParser;
41 : :
42 : 74 : CmdLineParser::CmdLineParser( int argc, char** argv,
43 : : const tk::Print& print,
44 : 74 : ctr::CmdLine& cmdline ) :
45 : 74 : StringParser( argc, argv )
46 : : // *****************************************************************************
47 : : // Contructor: parse the command line for Walker
48 : : //! \param[in] argc Number of C-style character arrays in argv
49 : : //! \param[in] argv C-style character array of character arrays
50 : : //! \param[in] print Pretty printer
51 : : //! \param[inout] cmdline Command-line stack where data is stored from parsing
52 : : // *****************************************************************************
53 : : {
54 : : // Create CmdLine (a tagged tuple) to store parsed input
55 [ + - ][ + - ]: 148 : ctr::CmdLine cmd( g_inputdeck.get< tag::cmd, tag::ctrinfo >() );
[ - - ]
56 : :
57 : : // Reset parser's output stream to that of print's. This is so that mild
58 : : // warnings emitted during parsing can be output using the pretty printer.
59 : : // Usually, errors and warnings are simply accumulated during parsing and
60 : : // printed during diagnostics after the parser has finished. However, in some
61 : : // special cases we can provide a more user-friendly message right during
62 : : // parsing since there is more information available to construct a more
63 : : // sensible message. This is done in e.g., tk::grm::store_option. Resetting
64 : : // the global g_print, to that of passed in as the constructor argument allows
65 : : // not to have to create a new pretty printer, but use the existing one.
66 [ + - ]: 74 : tk::grm::g_print.reset( print.save() );
67 : :
68 : : // Parse command line string by populating the underlying tagged tuple:
69 : : tao::pegtl::memory_input<> in( m_string, "command line" );
70 : : tao::pegtl::parse< cmd::read_string, tk::grm::action >( in, cmd );
71 : :
72 : : // Echo errors and warnings accumulated during parsing
73 [ + - ]: 74 : diagnostics( print, cmd.get< tag::error >() );
74 : :
75 : : // Strip command line (and its underlying tagged tuple) from PEGTL instruments
76 : : // and transfer it out
77 : : cmdline = std::move( cmd );
78 : :
79 : : // If we got here, the parser succeeded
80 [ + - ][ + - ]: 74 : print.item( "Parsed command line", "success" );
81 : :
82 : : // Print out help on all command-line arguments if the executable was invoked
83 : : // without arguments or the help was requested
84 : 74 : const auto helpcmd = cmdline.get< tag::help >();
85 [ - + ]: 74 : if (argc == 1 || helpcmd) {
86 [ - - ][ - - ]: 0 : print.help< tk::QUIET >( tk::walker_executable(),
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
87 : : cmdline.get< tag::cmdinfo >(),
88 : : "Command-line Parameters:", "-" );
89 [ - - ][ - - ]: 0 : print.mandatory< tk::QUIET >( "The '--" + kw::control().string() +
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
90 : : " <filename>' argument is mandatory." );
91 [ - - ][ - - ]: 0 : print.usage< tk::QUIET >(
92 [ - - ][ - - ]: 0 : tk::walker_executable(),
[ - - ]
93 [ - - ][ - - ]: 0 : "charmrun +p4 " + tk::walker_executable() + " -" +
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ]
94 [ - - ][ - - ]: 0 : *kw::verbose().alias() + " -" + *kw::control().alias() + " mixdir.q",
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
95 : : "will execute the simulation configured in the control file 'mixdir.q' "
96 : : "on 4 CPUs producing verbose screen output" );
97 : : }
98 : :
99 : : // Print out help on all control file keywords if they were requested
100 : 74 : const auto helpctr = cmdline.get< tag::helpctr >();
101 [ - + ]: 74 : if (helpctr)
102 [ - - ][ - - ]: 0 : print.help< tk::QUIET >( tk::walker_executable(),
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ]
103 : : cmdline.get< tag::ctrinfo >(),
104 : : "Control File Keywords:" );
105 : :
106 : : // Print out verbose help for a single keyword if requested
107 [ + - ]: 148 : const auto helpkw = cmdline.get< tag::helpkw >();
108 [ - + ]: 74 : if (!helpkw.keyword.empty())
109 [ - - ][ - - ]: 0 : print.helpkw< tk::QUIET >( tk::walker_executable(), helpkw );
110 : :
111 : : // Print out version information if it was requested
112 : 74 : const auto version = cmdline.get< tag::version >();
113 [ - + ]: 74 : if (version)
114 [ - - ][ - - ]: 0 : print.version< tk::QUIET >( tk::walker_executable(),
[ - - ][ - - ]
115 [ - - ][ - - ]: 0 : tk::quinoa_version(),
[ - - ]
116 [ - - ][ - - ]: 0 : tk::git_commit(),
[ - - ]
117 [ - - ]: 0 : tk::copyright() );
118 : :
119 : : // Print out license information if it was requested
120 : 74 : const auto license = cmdline.get< tag::license >();
121 [ - + ]: 74 : if (license)
122 [ - - ][ - - ]: 0 : print.license< tk::QUIET >( tk::walker_executable(), tk::license() );
[ - - ][ - - ]
[ - - ]
123 : :
124 : : // Immediately exit if any help was output or was called without any argument
125 : : // or version or license info was requested with zero exit code
126 [ + - ][ + - ]: 74 : if (argc == 1 || helpcmd || helpctr || !helpkw.keyword.empty() || version ||
[ + - ][ - + ]
127 : : license)
128 : : {
129 [ - - ]: 0 : CkExit();
130 : : }
131 : :
132 : : // Make sure mandatory arguments are set
133 [ + - ][ - + ]: 148 : auto alias = kw::control().alias();
134 [ - + ][ - - ]: 74 : ErrChk( !(cmdline.get< tag::io, tag::control >().empty()),
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ]
135 : : "Mandatory control file not specified. "
136 : : "Use '--" + kw::control().string() + " <filename>'" +
137 : : ( alias ? " or '-" + *alias + " <filename>'" : "" ) + '.' );
138 : 74 : }
|