Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Control/Inciter/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 Inciter's command line parser
9 : : \details This file defines the command-line argument parser for the
10 : : computational shock hydrodynamics tool, Inciter.
11 : : */
12 : : // *****************************************************************************
13 : :
14 : : #include "NoWarning/pegtl.hpp"
15 : : #include "NoWarning/charm.hpp"
16 : :
17 : : #include "QuinoaConfig.hpp"
18 : : #include "Exception.hpp"
19 : : #include "Print.hpp"
20 : : #include "Keywords.hpp"
21 : : #include "Inciter/CmdLine/Parser.hpp"
22 : : #include "Inciter/CmdLine/Grammar.hpp"
23 : : #include "Inciter/CmdLine/CmdLine.hpp"
24 : : #include "Inciter/InputDeck/InputDeck.hpp"
25 : :
26 : : namespace tk {
27 : : namespace grm {
28 : :
29 : : tk::Print g_print;
30 : :
31 : : } // grm::
32 : : } // tk::
33 : :
34 : : namespace inciter {
35 : :
36 : : extern ctr::InputDeck g_inputdeck;
37 : :
38 : : } // inciter::
39 : :
40 : : using inciter::CmdLineParser;
41 : :
42 : 189 : CmdLineParser::CmdLineParser( int argc, char** argv,
43 : : const tk::Print& print,
44 : 189 : ctr::CmdLine& cmdline ) :
45 : 189 : StringParser( argc, argv )
46 : : // *****************************************************************************
47 : : // Contructor: parse the command line for Inciter
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[in,out] cmdline Command-line stack where data is stored from parsing
52 : : // *****************************************************************************
53 : : {
54 : : // Create CmdLine (a tagged tuple) to store parsed input
55 [ + - ][ + - ]: 378 : 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 [ + - ][ + - ]: 189 : tk::grm::g_print.reset( print.save() );
67 : :
68 : : // Parse command line string by populating the underlying tagged tuple:
69 [ + - ]: 378 : tao::pegtl::memory_input<> in( m_string, "command line" );
70 [ + - ]: 189 : tao::pegtl::parse< cmd::read_string, tk::grm::action >( in, cmd );
71 : :
72 : : // Echo errors and warnings accumulated during parsing
73 [ + - ]: 189 : 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 : 189 : cmdline = std::move( cmd );
78 : :
79 : : // If we got here, the parser succeeded
80 [ + - ][ + - ]: 189 : 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 : 189 : const auto helpcmd = cmdline.get< tag::help >();
85 [ + - ][ - + ]: 189 : if (argc == 1 || helpcmd) {
86 [ - - ][ - - ]: 0 : print.help< tk::QUIET >( tk::inciter_executable(),
[ - - ][ - - ]
87 : 0 : cmdline.get< tag::cmdinfo >(),
88 : : "Command-line Parameters:", "-" );
89 [ - - ]: 0 : print.mandatory< tk::QUIET >(
90 [ - - ][ - - ]: 0 : "The '--" + kw::input().string() + " <filename>' and the "
[ - - ]
91 [ - - ][ - - ]: 0 : "'--" + kw::control().string() + " <filename>' arguments are mandatory." );
[ - - ]
92 [ - - ][ - - ]: 0 : print.usage< tk::QUIET >(
93 [ - - ]: 0 : tk::inciter_executable(),
94 [ - - ][ - - ]: 0 : "charmrun +p4 " + tk::inciter_executable() + " -" +
[ - - ][ - - ]
[ - - ]
95 [ - - ][ - - ]: 0 : *kw::verbose().alias() + " -" + *kw::control().alias() +
[ - - ][ - - ]
96 [ - - ][ - - ]: 0 : " vort.q -" + *kw::input().alias() + " unitcube.exo",
[ - - ]
97 : : "will execute the simulation configured in the control file 'vort.q' "
98 : : "using the mesh in 'unitcube.exo' on 4 CPUs producing verbose screen "
99 : : "output" );
100 : : }
101 : :
102 : : // Print out help on all control file keywords if they were requested
103 : 189 : const auto helpctr = cmdline.get< tag::helpctr >();
104 [ - + ]: 189 : if (helpctr)
105 [ - - ][ - - ]: 0 : print.help< tk::QUIET >( tk::inciter_executable(),
[ - - ][ - - ]
106 : 0 : cmdline.get< tag::ctrinfo >(),
107 : : "Control File Keywords:" );
108 : :
109 : : // Print out verbose help for a single keyword if requested
110 [ + - ]: 378 : const auto helpkw = cmdline.get< tag::helpkw >();
111 [ - + ]: 189 : if (!helpkw.keyword.empty())
112 [ - - ][ - - ]: 0 : print.helpkw< tk::QUIET >( tk::inciter_executable(), helpkw );
113 : :
114 : : // Print out version information if it was requested
115 : 189 : const auto version = cmdline.get< tag::version >();
116 [ - + ]: 189 : if (version)
117 [ - - ][ - - ]: 0 : print.version< tk::QUIET >( tk::inciter_executable(),
118 [ - - ]: 0 : tk::quinoa_version(),
119 [ - - ]: 0 : tk::git_commit(),
120 [ - - ]: 0 : tk::copyright() );
121 : :
122 : : // Print out license information if it was requested
123 : 189 : const auto license = cmdline.get< tag::license >();
124 [ - + ]: 189 : if (license)
125 [ - - ][ - - ]: 0 : print.license< tk::QUIET >( tk::inciter_executable(), tk::license() );
[ - - ]
126 : :
127 : : // Immediately exit if any help was output or was called without any argument
128 : : // or version or license info was requested with zero exit code
129 [ + - ][ + - ]: 189 : if (argc == 1 || helpcmd || helpctr || !helpkw.keyword.empty() || version ||
[ + - ][ + - ]
[ + - ][ - + ]
[ - + ]
130 : : license)
131 : : {
132 [ - - ]: 0 : CkExit();
133 : : }
134 : :
135 : : // Make sure mandatory arguments are set
136 [ + - ]: 378 : auto ctralias = kw::control().alias();
137 [ - + ][ - - ]: 189 : ErrChk( !(cmdline.get< tag::io, tag::control >().empty()),
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
138 : : "Mandatory control file not specified. "
139 : : "Use '--" + kw::control().string() + " <filename>'" +
140 : : ( ctralias ? " or '-" + *ctralias + " <filename>'" : "" ) + '.' );
141 : 189 : }
|