KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command.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) 2022 Mark Roszko <[email protected]>
5 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "command.h"
22#include <cli/exit_codes.h>
23#include <wx/crt.h>
24#include <macros.h>
25#include <string_utils.h>
26
27#include <sstream>
28
29
30CLI::COMMAND::COMMAND( const std::string& aName ) :
31 m_name( aName ),
32 m_argParser( aName, "", argparse::default_arguments::none ),
33 m_hasInputArg( false ),
34 m_hasOutputArg( false ),
35 m_hasDrawingSheetArg( false ),
36 m_hasDefineArg( false ),
37 m_outputArgExpectsDir( false )
38
39{
40 m_argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
41 .default_value( false )
42 .help( UTF8STDSTR( ARG_HELP_DESC ) )
43 .implicit_value( true )
44 .nargs( 0 );
45}
46
47
49{
50 std::stringstream ss;
51 ss << m_argParser;
52 wxPrintf( From_UTF8( ss.str().c_str() ) );
53}
54
55
57{
58 if( m_argParser[ ARG_HELP ] == true )
59 {
60 PrintHelp();
61
62 return 0;
63 }
64
65 if ( m_hasInputArg )
66 {
67 m_argInput = From_UTF8( m_argParser.get<std::string>( ARG_INPUT ).c_str() );
68 }
69
70 if( m_hasOutputArg )
71 {
72 m_argOutput = From_UTF8( m_argParser.get<std::string>( ARG_OUTPUT ).c_str() );
73 }
74
75 if( m_hasDrawingSheetArg )
76 {
77 m_argDrawingSheet = From_UTF8( m_argParser.get<std::string>( ARG_DRAWING_SHEET ).c_str() );
78 }
79
80
81 if( m_hasDefineArg )
82 {
83 auto defines = m_argParser.get<std::vector<std::string>>( ARG_DEFINE_VAR_LONG );
84
85 for( const std::string& def : defines )
86 {
87 wxString str = From_UTF8( def.c_str() );
88 wxArrayString bits;
89 wxStringSplit( str, bits, wxS( '=' ) );
90
91 if( bits.Count() == 2 )
92 {
93 m_argDefineVars[bits[0]] = bits[1];
94 }
95 else
96 {
98 }
99 }
100 }
101
102 return doPerform( aKiway );
103}
104
105
107{
108 // default case if we aren't overloaded, just print the help
109 PrintHelp();
110
111 return EXIT_CODES::OK;
112}
113
114
115void CLI::COMMAND::addCommonArgs( bool aInput, bool aOutput, bool aInputIsDir, bool aOutputIsDir )
116{
117 m_hasInputArg = aInput;
118 m_hasOutputArg = aOutput;
119 m_outputArgExpectsDir = aOutputIsDir;
120
121 if( aInput )
122 {
123 if( aInputIsDir )
124 {
125 m_argParser.add_argument( ARG_INPUT )
126 .help( UTF8STDSTR( _( "Input directory" ) ) )
127 .metavar( "INPUT_DIR" );
128 }
129 else
130 {
131 m_argParser.add_argument( ARG_INPUT )
132 .help( UTF8STDSTR( _( "Input file" ) ) )
133 .metavar( "INPUT_FILE" );
134 }
135 }
136
137 if( aOutput )
138 {
139 if( aOutputIsDir )
140 {
141 m_argParser.add_argument( "-o", ARG_OUTPUT )
142 .default_value( std::string() )
143 .help( UTF8STDSTR( _( "Output directory" ) ) )
144 .metavar( "OUTPUT_DIR" );
145 }
146 else
147 {
148 m_argParser.add_argument( "-o", ARG_OUTPUT )
149 .default_value( std::string() )
150 .help( UTF8STDSTR( _( "Output file" ) ) )
151 .metavar( "OUTPUT_FILE" );
152 }
153 }
154}
155
156
158{
159 m_hasDrawingSheetArg = true;
160
161 m_argParser.add_argument( ARG_DRAWING_SHEET )
162 .default_value( std::string() )
163 .help( UTF8STDSTR( _( "Path to drawing sheet, this overrides any existing project "
164 "defined sheet when used" ) ) )
165 .metavar( "SHEET_PATH" );
166}
167
168
170{
171 m_hasDefineArg = true;
172
173 m_argParser.add_argument( ARG_DEFINE_VAR_LONG, ARG_DEFINE_VAR_SHORT )
174 .default_value( std::vector<std::string>() )
175 .append()
176 .help( UTF8STDSTR(
177 _( "Overrides or adds project variables, can be used multiple times to "
178 "declare multiple variables."
179 "\nUse in the format of '--define-var key=value' or '-D key=value'" ) ) )
180 .metavar( "KEY=VALUE" );
181}
virtual int doPerform(KIWAY &aKiway)
The internal handler that should be overloaded to implement command specific processing and work.
Definition: command.cpp:106
argparse::ArgumentParser m_argParser
Definition: command.h:100
COMMAND(const std::string &aName)
Define a new COMMAND instance.
Definition: command.cpp:30
void addDefineArg()
Set up the drawing sheet arg used by many of the export commands.
Definition: command.cpp:169
void PrintHelp()
Definition: command.cpp:48
int Perform(KIWAY &aKiway)
Entry point to processing commands from args and doing work.
Definition: command.cpp:56
void addCommonArgs(bool aInput, bool aOutput, bool aInputIsDir, bool aOutputIsDir)
Set up the most common of args used across cli.
Definition: command.cpp:115
void addDrawingSheetArg()
Set up the drawing sheet arg used by many of the export commands.
Definition: command.cpp:157
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:284
#define ARG_HELP
Definition: command.h:30
#define ARG_OUTPUT
Definition: command.h:33
#define ARG_INPUT
Definition: command.h:34
#define UTF8STDSTR(s)
Definition: command.h:27
#define ARG_HELP_DESC
Definition: command.h:32
#define ARG_DRAWING_SHEET
Definition: command.h:35
#define ARG_DEFINE_VAR_LONG
Definition: command.h:37
#define ARG_DEFINE_VAR_SHORT
Definition: command.h:36
#define ARG_HELP_SHORT
Definition: command.h:31
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static const int ERR_ARGS
Definition: exit_codes.h:31
static const int OK
Definition: exit_codes.h:30
wxString From_UTF8(const char *cstring)
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.