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 The 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, see <https://www.gnu.org/licenses/>.
18 */
21
22#include <wx/msgout.h>
23
24namespace KI_TEST
25{
26
27void COMBINED_UTILITY::showSubUtilityList( std::ostream& os ) const
28{
29 for( const auto& it : UTILITY_REGISTRY::GetInfoMap() )
30 {
31 const UTILITY_PROGRAM& prog = it.second;
32
33 os << "Reg: " << prog.m_name << ": \t" << prog.m_desc << std::endl;
34 }
35}
36
37
39{
40 try
41 {
43
44 return &prog.m_func;
45 }
46 catch( const std::out_of_range& )
47 {
48 // not found in map
49 }
50
51 return nullptr;
52}
53
54
55void COMBINED_UTILITY::printUsage( char* name, std::ostream& os ) const
56{
57 os << "Run a utility tool." << std::endl;
58
59 os << "Usage: " << name << " [-h] [-l] [TOOL [TOOL_OPTIONS]]" << std::endl;
60
61 os << " -h show this message and exit." << std::endl
62 << " -l print known tools and exit." << std::endl;
63
64 os << std::endl;
65 os << "Known tools: " << std::endl;
66
68}
69
70
71int COMBINED_UTILITY::HandleCommandLine( int argc, char** argv ) const
72{
73 wxMessageOutput::Set( new wxMessageOutputStderr );
74
75 // Need at least one parameter
76 if( argc < 2 )
77 {
78 printUsage( argv[0], std::cerr );
80 }
81
82 const std::string arg1( argv[1] );
83
84 if( argc == 2 )
85 {
86 if( arg1 == "-h" )
87 {
88 printUsage( argv[0], std::cout );
89 return RET_CODES::OK;
90 }
91 else if( arg1 == "-l" )
92 {
93 showSubUtilityList( std::cout );
94 return RET_CODES::OK;
95 }
96 }
97
98 auto func = findSubUtility( arg1 );
99
100 if( !func )
101 {
103 }
104
105 // pass on the rest of the commands
106 return ( *func )( argc - 1, argv + 1 );
107}
108
109} // namespace KI_TEST
const char * name
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.