KiCad PCB EDA Suite
KI_TEST::COMBINED_UTILITY Class Reference

Class that handles delegation of command lines to one of a number of "sub-utilities". More...

#include <utility_program.h>

Public Member Functions

int HandleCommandLine (int argc, char **argv) const
 Take in a command line and: More...
 

Private Member Functions

void showSubUtilityList (std::ostream &os) const
 Format the list of known sub-utils. More...
 
UTILITY_PROGRAM::FUNCfindSubUtility (const std::string &aName) const
 Find a sub-utility with the given ID/name. More...
 
void printUsage (char *name, std::ostream &os) const
 Print the command line usage of this program. More...
 

Detailed Description

Class that handles delegation of command lines to one of a number of "sub-utilities".

Definition at line 97 of file utility_program.h.

Member Function Documentation

◆ findSubUtility()

UTILITY_PROGRAM::FUNC * KI_TEST::COMBINED_UTILITY::findSubUtility ( const std::string &  aName) const
private

Find a sub-utility with the given ID/name.

Parameters
aNamethe desired sub-utility name (e.g. "drc")
Returns
pointer to the function that runs that sub-utility

Definition at line 42 of file utility_program.cpp.

43{
44 try
45 {
46 UTILITY_PROGRAM& prog = UTILITY_REGISTRY::GetInfoMap().at( aName );
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}
static PLUGIN_MAP & GetInfoMap()
Accessor for the static registry map.

References UTILITY_REGISTRY::GetInfoMap(), and KI_TEST::UTILITY_PROGRAM::m_func.

Referenced by HandleCommandLine().

◆ HandleCommandLine()

int KI_TEST::COMBINED_UTILITY::HandleCommandLine ( int  argc,
char **  argv 
) const

Take in a command line and:

  • Handle "top level" commands like -h and -l
  • Delegate to sub-utilities
  • Report malformed command lines
Parameters
argcargument count (directly from the main() parameter )
argvargument values (directly from the main() parameter )
Returns
return code

Definition at line 75 of file utility_program.cpp.

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}
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.
#define OK
@ UNKNOWN_TOOL
The tool asked for was not found.
@ BAD_CMDLINE
The command line was not correct for the tool.

References KI_TEST::BAD_CMDLINE, findSubUtility(), OK, printUsage(), showSubUtilityList(), and KI_TEST::UNKNOWN_TOOL.

Referenced by main(), and APP_TEST::OnInit().

◆ printUsage()

void KI_TEST::COMBINED_UTILITY::printUsage ( char *  name,
std::ostream &  os 
) const
private

Print the command line usage of this program.

Parameters
namethe name the program was run with
osstream to print to

Definition at line 59 of file utility_program.cpp.

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}
const char * name
Definition: DXF_plotter.cpp:56

References name, and showSubUtilityList().

Referenced by HandleCommandLine().

◆ showSubUtilityList()

void KI_TEST::COMBINED_UTILITY::showSubUtilityList ( std::ostream &  os) const
private

Format the list of known sub-utils.

Parameters
osthe stream to format on

Definition at line 31 of file utility_program.cpp.

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}

References UTILITY_REGISTRY::GetInfoMap(), KI_TEST::UTILITY_PROGRAM::m_desc, and KI_TEST::UTILITY_PROGRAM::m_name.

Referenced by HandleCommandLine(), and printUsage().


The documentation for this class was generated from the following files: