1 /******************************************************************************
2 * Copyright (c) 2018, Hobu Inc. (info@hobu.co)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #include <pdal/SubcommandKernel.hpp>
36 #include <pdal/util/Algorithm.hpp>
37 
38 namespace pdal
39 {
40 
doSwitches(const StringList & cmdArgs,ProgramArgs & args)41 bool SubcommandKernel::doSwitches(const StringList& cmdArgs, ProgramArgs& args)
42 {
43     StringList stringArgs = extractStageOptions(cmdArgs);
44 
45     try
46     {
47         bool help;
48 
49         // parseSimple allows us to scan for the help option without
50         // raising exception about missing arguments and so on.
51         // It also removes consumed args from the arg list, so parse a copy
52         // that will be ignored by parse() below.
53         // Also search for a subcommand.
54         ProgramArgs hargs;
55         const StringList& subs = subcommands();
56         assert(subs.size());
57         hargs.add("help,h", "Print help message", help);
58         hargs.add("subcommand", "Subcommand for application", m_subcommand).
59             setPositional();
60         hargs.parseSimple(stringArgs);
61         if (help)
62             return false;
63 
64         if (m_subcommand.empty())
65             throw pdal_error(getName() + ": no subcommand found.");
66         if (!Utils::contains(subs, m_subcommand))
67             throw pdal_error(getName() + ": invalid subcommand '" +
68                 m_subcommand + "'.");
69 
70         addBasicSwitches(args);
71         addSubSwitches(args, m_subcommand);
72         args.parse(stringArgs);
73     }
74     catch (arg_error& e)
75     {
76         throw pdal_error(getName() + ": " + e.m_error);
77     }
78     return true;
79 }
80 
81 
outputHelp()82 void SubcommandKernel::outputHelp()
83 {
84     auto outputStdOpts = [this]()
85     {
86         ProgramArgs args;
87         addBasicSwitches(args);
88 
89         std::cout << "standard options:" << std::endl;
90         args.dump(std::cout, 2, Utils::screenWidth());
91     };
92 
93     auto outputSubOpts = [this](const std::string& sub)
94     {
95         ProgramArgs args;
96         addSubSwitches(args, sub);
97 
98         std::cout << "subcommand '" << sub << "' options:" << std::endl;
99         args.dump(std::cout, 2, Utils::screenWidth());
100     };
101 
102     if (Utils::contains(subcommands(), m_subcommand))
103     {
104         // We're repeating addSubSwitches here (it's also called from
105         // outputSubOpts), but it keeps things cleaner since outputSubOpts
106         // also writes output.
107         ProgramArgs args;
108         addSubSwitches(args, m_subcommand);
109 
110         std::cout << "usage: " << "pdal " << getShortName() << " " <<
111             m_subcommand << " [options] " << args.commandLine() <<
112             std::endl;
113 
114         outputStdOpts();
115         outputSubOpts(m_subcommand);
116     }
117     else
118     {
119         std::cout << "usage: " << "pdal " << getShortName() <<
120             " <subcommand> [options] " << std::endl;
121 
122         outputStdOpts();
123         for (const std::string& subcmd : subcommands())
124             outputSubOpts(subcmd);
125     }
126 
127     std::cout << "\nFor more information, see the full documentation for "
128         "PDAL at https://pdal.io/\n" << std::endl;
129 }
130 
131 } // namespace pdal
132 
133