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 The 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
18 * along with this program. If not, see <https://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_hasVariantArg( false ),
39
40{
41 m_argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
43 .flag()
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 if( m_hasDefineArg )
81 {
82 std::vector<std::string> defines = m_argParser.get<std::vector<std::string>>( ARG_DEFINE_VAR_LONG );
84 for( const std::string& def : defines )
85 {
86 wxString str = From_UTF8( def.c_str() );
87 wxArrayString bits;
88 wxStringSplit( str, bits, wxS( '=' ) );
89
90 if( bits.Count() == 2 )
91 {
92 m_argDefineVars[bits[0]] = bits[1];
93 }
94 else
95 {
97 }
98 }
99 }
100
101 if( m_hasVariantArg && m_argParser.is_used( ARG_VARIANT ) )
102 {
103 std::vector<std::string> variantNames = m_argParser.get<std::vector<std::string>>( ARG_VARIANT );
104
105 for( const std::string& name : variantNames )
106 m_argVariantNames.push_back( From_UTF8( name ) );
107
108 if( m_variantsRequireOutputToken
109 && m_argVariantNames.size() > 1
110 && m_hasOutputArg
111 && !m_argOutput.IsEmpty() )
112 {
113 if( !m_argOutput.Contains( wxS( "${VARIANT}" ) ) )
114 {
115 wxFprintf( stderr, _( "When specifying multiple variants, the output path must contain "
116 "${VARIANT} to generate separate output files for each variant.\n" ) );
118 }
119 }
120 }
121
122 return doPerform( aKiway );
123}
124
125
127{
128 // default case if we aren't overloaded, just print the help
129 PrintHelp();
130
131 return EXIT_CODES::OK;
132}
133
134
135void CLI::COMMAND::addCommonArgs( bool aInput, bool aOutput, IO_TYPE aInputType, IO_TYPE aOutputType )
136{
137 m_hasInputArg = aInput;
138 m_hasOutputArg = aOutput;
139
140 if( aInput )
141 {
142 switch( aInputType )
143 {
144 case IO_TYPE::FILE:
145 {
146 m_argParser.add_argument( ARG_INPUT )
147 .help( UTF8STDSTR( _( "Input file" ) ) )
148 .metavar( "INPUT_FILE" );
149 break;
150 }
152 {
153 m_argParser.add_argument( ARG_INPUT )
154 .help( UTF8STDSTR( _( "Input directory" ) ) )
155 .metavar( "INPUT_DIR" );
156 break;
157 }
159 {
160 m_argParser.add_argument( ARG_INPUT )
161 .help( UTF8STDSTR( _( "Input file or directory" ) ) )
162 .metavar( "INPUT_FILE_OR_DIR" );
163 break;
164 }
165 // no default
166 }
167 }
168
169 if( aOutput )
170 {
171 switch( aOutputType )
172 {
173 case IO_TYPE::FILE:
174 {
175 m_argParser.add_argument( "-o", ARG_OUTPUT )
176 .default_value( std::string() )
177 .help( UTF8STDSTR( _( "Output file" ) ) )
178 .metavar( "OUTPUT_FILE" );
179 break;
180 }
182 {
183 m_argParser.add_argument( "-o", ARG_OUTPUT )
184 .default_value( std::string() )
185 .help( UTF8STDSTR( _( "Output directory" ) ) )
186 .metavar( "OUTPUT_DIR" );
187 break;
188 }
190 {
191 m_argParser.add_argument( "-o", ARG_OUTPUT )
192 .default_value( std::string() )
193 .help( UTF8STDSTR( _( "Output file or directory" ) ) )
194 .metavar( "OUTPUT_FILE_OR_DIR" );
195 break;
196 }
197 // no default
198 }
199 }
200}
201
202
204{
206
207 m_argParser.add_argument( ARG_DRAWING_SHEET )
208 .default_value( std::string() )
209 .help( UTF8STDSTR( _( "Path to drawing sheet, this overrides any existing project "
210 "defined sheet when used" ) ) )
211 .metavar( "SHEET_PATH" );
212}
213
214
216{
217 m_hasDefineArg = true;
218
220 .default_value( std::vector<std::string>() )
221 .append()
222 .help( UTF8STDSTR( _( "Overrides or adds project variables, can be used multiple times to "
223 "declare multiple variables.\n"
224 "Use in the format of '--define-var key=value' or '-D key=value'" ) ) )
225 .metavar( "KEY=VALUE" );
226}
227
228
229void CLI::COMMAND::addVariantsArg( bool aRequireOutputToken )
230{
231 m_hasVariantArg = true;
232 m_variantsRequireOutputToken = aRequireOutputToken;
233
234 m_argParser.add_argument( ARG_VARIANT )
235 .default_value( std::vector<std::string>() )
236 .append()
237 .help( UTF8STDSTR( _( "The variant name(s) to output, can be used multiple times to specify "
238 "multiple variants.\n"
239 "When specifying multiple variants, use ${VARIANT} in the output path to "
240 "generate separate files for each variant.\n"
241 "When no --variant argument is provided, the default variant is output." ) ) );
242}
const char * name
std::string m_name
Name of this command that is exported and used in the cli.
Definition command.h:111
std::map< wxString, wxString > m_argDefineVars
Definition command.h:153
virtual int doPerform(KIWAY &aKiway)
The internal handler that should be overloaded to implement command specific processing and work.
Definition command.cpp:126
bool m_hasDefineArg
Whether or not the input arg was added for parsing.
Definition command.h:133
argparse::ArgumentParser m_argParser
Definition command.h:113
bool m_hasOutputArg
Whether or not the output arg was added for parsing.
Definition command.h:123
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:215
void addVariantsArg(bool aRequireOutputToken=true)
Set up the list of variants to output arguement.
Definition command.cpp:229
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, IO_TYPE aInputType, IO_TYPE aOutputType)
Set up the most common of args used across cli.
Definition command.cpp:135
bool m_hasInputArg
Whether or not the input arg was added for parsing.
Definition command.h:118
void addDrawingSheetArg()
Set up the drawing sheet arg used by many of the export commands.
Definition command.cpp:203
bool m_hasVariantArg
Whether or not the input argument for variant names was added for parsing.
Definition command.h:158
bool m_hasDrawingSheetArg
Whether or not the input arg was added for parsing.
Definition command.h:128
bool m_variantsRequireOutputToken
Definition command.h:159
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
#define ARG_HELP
Definition command.h:30
#define ARG_OUTPUT
Definition command.h:33
#define ARG_INPUT
Definition command.h:34
#define ARG_VARIANT
Definition command.h:38
#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)
static wxString From_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition idf_helpers.h:44
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
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.