KiCad PCB EDA Suite
Loading...
Searching...
No Matches
utility_program.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
25
26#include <wx/msgout.h>
27
28namespace KI_TEST
29{
30
31void COMBINED_UTILITY::showSubUtilityList( std::ostream& os ) const
32{
33 for( const auto& it : UTILITY_REGISTRY::GetInfoMap() )
34 {
35 const UTILITY_PROGRAM& prog = it.second;
36
37 os << "Reg: " << prog.m_name << ": \t" << prog.m_desc << std::endl;
38 }
39}
40
41
43{
44 try
45 {
47
48 return &prog.m_func;
49 }
50 catch( const std::out_of_range& )
51 {
52 // not found in map
53 }
54
55 return nullptr;
56}
57
58
59void COMBINED_UTILITY::printUsage( char* name, std::ostream& os ) const
60{
61 os << "Run a utility tool." << std::endl;
62
63 os << "Usage: " << name << " [-h] [-l] [TOOL [TOOL_OPTIONS]]" << std::endl;
64
65 os << " -h show this message and exit." << std::endl
66 << " -l print known tools and exit." << std::endl;
67
68 os << std::endl;
69 os << "Known tools: " << std::endl;
70
72}
73
74
75int COMBINED_UTILITY::HandleCommandLine( int argc, char** argv ) const
76{
77 wxMessageOutput::Set( new wxMessageOutputStderr );
78
79 // Need at least one parameter
80 if( argc < 2 )
81 {
82 printUsage( argv[0], std::cerr );
84 }
85
86 const std::string arg1( argv[1] );
87
88 if( argc == 2 )
89 {
90 if( arg1 == "-h" )
91 {
92 printUsage( argv[0], std::cout );
93 return RET_CODES::OK;
94 }
95 else if( arg1 == "-l" )
96 {
97 showSubUtilityList( std::cout );
98 return RET_CODES::OK;
99 }
100 }
101
102 auto func = findSubUtility( arg1 );
103
104 if( !func )
105 {
107 }
108
109 // pass on the rest of the commands
110 return ( *func )( argc - 1, argv + 1 );
111}
112
113} // namespace KI_TEST
const char * name
Definition: DXF_plotter.cpp:57
UTILITY_PROGRAM::FUNC * findSubUtility(const std::string &aName) const
Find a sub-utility with the given ID/name.
void showSubUtilityList(std::ostream &os) const
Format the list of known sub-utils.
void printUsage(char *name, std::ostream &os) const
Print the command line usage of this program.
int HandleCommandLine(int argc, char **argv) const
Take in a command line and:
static PLUGIN_MAP & GetInfoMap()
Accessor for the static registry map.
@ OK
Tool exited OK.
@ UNKNOWN_TOOL
The tool asked for was not found.
@ BAD_CMDLINE
The command line was not correct for the tool.
Description of a "utility program", which is a program that takes some command line and does "somethi...
std::function< int(int argc, char **argv)> FUNC
A function that provides the program for a given command line.
std::string m_name
The name of the program (this is used to select one)
FUNC m_func
The function to call to run the program.
std::string m_desc
Description of the program.