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 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_hasVariantArg( false )
38
39{
40 m_argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
42 .flag()
43 .nargs( 0 );
44}
45
46
48{
49 std::stringstream ss;
50 ss << m_argParser;
51 wxPrintf( From_UTF8( ss.str().c_str() ) );
52}
53
54
56{
57 if( m_argParser[ ARG_HELP ] == true )
58 {
59 PrintHelp();
60
61 return 0;
62 }
63
64 if ( m_hasInputArg )
65 {
66 m_argInput = From_UTF8( m_argParser.get<std::string>( ARG_INPUT ).c_str() );
67 }
68
69 if( m_hasOutputArg )
70 {
71 m_argOutput = From_UTF8( m_argParser.get<std::string>( ARG_OUTPUT ).c_str() );
72 }
73
75 {
76 m_argDrawingSheet = From_UTF8( m_argParser.get<std::string>( ARG_DRAWING_SHEET ).c_str() );
77 }
78
79 if( m_hasDefineArg )
80 {
81 auto defines = m_argParser.get<std::vector<std::string>>( ARG_DEFINE_VAR_LONG );
82
83 for( const std::string& def : defines )
84 {
85 wxString str = From_UTF8( def.c_str() );
86 wxArrayString bits;
87 wxStringSplit( str, bits, wxS( '=' ) );
88
89 if( bits.Count() == 2 )
90 {
91 m_argDefineVars[bits[0]] = bits[1];
92 }
93 else
94 {
96 }
97 }
98 }
99
100 if( m_hasVariantArg && m_argParser.is_used( ARG_VARIANT ) )
101 {
102 auto variantNames = m_argParser.get<std::vector<std::string>>( ARG_VARIANT );
103
104 for( const auto& name : variantNames )
105 m_argVariantNames.push_back( From_UTF8( name ) );
106
107 if( m_argVariantNames.size() > 1 && m_hasOutputArg && !m_argOutput.IsEmpty() )
108 {
109 if( !m_argOutput.Contains( wxS( "${VARIANT}" ) ) )
110 {
111 wxFprintf( stderr,
112 _( "When specifying multiple variants, the output path must contain "
113 "${VARIANT} to generate separate output files for each variant.\n" ) );
115 }
116 }
117 }
118
119 return doPerform( aKiway );
120}
121
122
124{
125 // default case if we aren't overloaded, just print the help
126 PrintHelp();
127
128 return EXIT_CODES::OK;
129}
130
131
132void CLI::COMMAND::addCommonArgs( bool aInput, bool aOutput, IO_TYPE aInputType, IO_TYPE aOutputType )
133{
134 m_hasInputArg = aInput;
135 m_hasOutputArg = aOutput;
136
137 if( aInput )
138 {
139 switch( aInputType )
140 {
141 case IO_TYPE::FILE:
142 {
143 m_argParser.add_argument( ARG_INPUT )
144 .help( UTF8STDSTR( _( "Input file" ) ) )
145 .metavar( "INPUT_FILE" );
146 break;
147 }
149 {
150 m_argParser.add_argument( ARG_INPUT )
151 .help( UTF8STDSTR( _( "Input directory" ) ) )
152 .metavar( "INPUT_DIR" );
153 break;
154 }
156 {
157 m_argParser.add_argument( ARG_INPUT )
158 .help( UTF8STDSTR( _( "Input file or directory" ) ) )
159 .metavar( "INPUT_FILE_OR_DIR" );
160 break;
161 }
162 // no default
163 }
164 }
165
166 if( aOutput )
167 {
168 switch( aOutputType )
169 {
170 case IO_TYPE::FILE:
171 {
172 m_argParser.add_argument( "-o", ARG_OUTPUT )
173 .default_value( std::string() )
174 .help( UTF8STDSTR( _( "Output file" ) ) )
175 .metavar( "OUTPUT_FILE" );
176 break;
177 }
179 {
180 m_argParser.add_argument( "-o", ARG_OUTPUT )
181 .default_value( std::string() )
182 .help( UTF8STDSTR( _( "Output directory" ) ) )
183 .metavar( "OUTPUT_DIR" );
184 break;
185 }
187 {
188 m_argParser.add_argument( "-o", ARG_OUTPUT )
189 .default_value( std::string() )
190 .help( UTF8STDSTR( _( "Output file or directory" ) ) )
191 .metavar( "OUTPUT_FILE_OR_DIR" );
192 break;
193 }
194 // no default
195 }
196 }
197}
198
199
201{
203
204 m_argParser.add_argument( ARG_DRAWING_SHEET )
205 .default_value( std::string() )
206 .help( UTF8STDSTR( _( "Path to drawing sheet, this overrides any existing project "
207 "defined sheet when used" ) ) )
208 .metavar( "SHEET_PATH" );
209}
210
211
213{
214 m_hasDefineArg = true;
215
217 .default_value( std::vector<std::string>() )
218 .append()
219 .help( UTF8STDSTR(
220 _( "Overrides or adds project variables, can be used multiple times to "
221 "declare multiple variables."
222 "\nUse in the format of '--define-var key=value' or '-D key=value'" ) ) )
223 .metavar( "KEY=VALUE" );
224}
225
226
228{
229 m_hasVariantArg = true;
230
231 m_argParser.add_argument( ARG_VARIANT )
232 .default_value( std::vector<std::string>() )
233 .append()
234 .help( UTF8STDSTR(
235 _( "The variant name(s) to output, can be used multiple times to specify "
236 "multiple variants.\n"
237 "When specifying multiple variants, use ${VARIANT} in the output path to "
238 "generate separate files for each variant.\n"
239 "When no --variant argument is provided the default variant is output." ) ) );
240}
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
Value of the drawing sheet arg if configured.
Definition command.h:158
virtual int doPerform(KIWAY &aKiway)
The internal handler that should be overloaded to implement command specific processing and work.
Definition command.cpp:123
bool m_hasDefineArg
Whether or not the input arg was added for parsing.
Definition command.h:133
void addVariantsArg()
Set up the list of variants to output arguement.
Definition command.cpp:227
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:212
void PrintHelp()
Definition command.cpp:47
int Perform(KIWAY &aKiway)
Entry point to processing commands from args and doing work.
Definition command.cpp:55
wxString m_argDrawingSheet
Value of the drawing sheet arg if configured.
Definition command.h:153
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:132
wxString m_argOutput
Value of the output arg if configured.
Definition command.h:148
wxString m_argInput
Value of the common input arg if configured.
Definition command.h:143
std::vector< wxString > m_argVariantNames
The list of variant names to output.
Definition command.h:170
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:200
bool m_hasVariantArg
Whether or not the input argument for variant names was added for parsing.
Definition command.h:163
bool m_hasDrawingSheetArg
Whether or not the input arg was added for parsing.
Definition command.h:128
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:295
#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)
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.