KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_cli.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) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26#include <wx/filename.h>
27#include <wx/log.h>
28#include <wx/stdpaths.h>
29#include <wx/wxcrtvararg.h> //for wxPrintf
30
31#include <kiway.h>
32#include <string_utils.h>
33#include <paths.h>
36#include <systemdirsappend.h>
37#include <trace_helpers.h>
38
39#include <stdexcept>
40
41#include "pgm_kicad.h"
42#include "kicad_manager_frame.h"
43
44#include <build_version.h>
45#include <kiplatform/app.h>
47#include <locale_io.h>
48
49#include "cli/command_pcb.h"
51#include "cli/command_pcb_drc.h"
64#include "cli/command_fp.h"
68#include "cli/command_sch.h"
69#include "cli/command_sch_erc.h"
71#include "cli/command_sym.h"
75#include "cli/command_version.h"
76#include "cli/exit_codes.h"
77
78// Add this header after all others, to avoid a collision name in a Windows header
79// on mingw.
80#include <wx/app.h>
81
82// a dummy to quiet linking with EDA_BASE_FRAME::config();
83#include <kiface_base.h>
85{
86 // This function should never be called. It is only referenced from
87 // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
88 // not to be actually called.
89 wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
90
91 throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
92}
93
94
96
97
99{
100 return program;
101}
102
103
104// Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
106{
107 return &program;
108}
109
110
112{
113 return program;
114}
115
116
118{
120
121 std::vector<COMMAND_ENTRY> subCommands;
122
123 COMMAND_ENTRY( CLI::COMMAND* aHandler ) : handler( aHandler ){};
124 COMMAND_ENTRY( CLI::COMMAND* aHandler, std::vector<COMMAND_ENTRY> aSub ) :
125 handler( aHandler ), subCommands( aSub ){};
126};
127
161
162
163static std::vector<COMMAND_ENTRY> commandStack = {
164 {
165 &fpCmd,
166 {
167 {
169 {
171 }
172 },
173 {
175 }
176 }
177 },
178 {
179 &pcbCmd,
180 {
181 {
182 &pcbDrcCmd
183 },
184 {
186 {
197 }
198 }
199 }
200 },
201 {
202 &schCmd,
203 {
204 {
205 &schErcCmd
206 },
207 {
209 {
218 }
219 }
220 }
221 },
222 {
223 &symCmd,
224 {
225 {
227 {
229 }
230 },
231 {
233 }
234 }
235 },
236 {
237 &versionCmd,
238 }
239};
240
241
242static void recurseArgParserBuild( argparse::ArgumentParser& aArgParser, COMMAND_ENTRY& aEntry )
243{
244 aArgParser.add_subparser( aEntry.handler->GetArgParser() );
245
246 for( COMMAND_ENTRY& subEntry : aEntry.subCommands )
247 {
248 recurseArgParserBuild( aEntry.handler->GetArgParser(), subEntry );
249 }
250}
251
252
253static COMMAND_ENTRY* recurseArgParserSubCommandUsed( argparse::ArgumentParser& aArgParser,
254 COMMAND_ENTRY& aEntry )
255{
256 COMMAND_ENTRY* cliCmd = nullptr;
257
258 if( aArgParser.is_subcommand_used( aEntry.handler->GetName() ) )
259 {
260 for( COMMAND_ENTRY& subentry : aEntry.subCommands )
261 {
262 cliCmd = recurseArgParserSubCommandUsed( aEntry.handler->GetArgParser(), subentry );
263 if( cliCmd )
264 break;
265 }
266
267 if(!cliCmd)
268 cliCmd = &aEntry;
269 }
270
271 return cliCmd;
272}
273
274
275static void printHelp( argparse::ArgumentParser& argParser )
276{
277 std::stringstream ss;
278 ss << argParser;
279 wxPrintf( From_UTF8( ss.str().c_str() ) );
280}
281
282
284{
286 App().SetAppDisplayName( wxT( "kicad-cli" ) );
287
288#if defined( DEBUG )
289 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
290
291 if( !wxIsAbsolutePath( absoluteArgv0 ) )
292 {
293 wxLogError( wxT( "No meaningful argv[0]" ) );
294 return false;
295 }
296#endif
297
298 if( !InitPgm( true, true) )
299 return false;
300
304 m_bm.Init();
305
306 return true;
307}
308
309
311{
312 argparse::ArgumentParser argParser( std::string( "kicad-cli" ), GetMajorMinorVersion().ToStdString(),
313 argparse::default_arguments::none );
314
315 argParser.add_argument( "-v", ARG_VERSION )
316 .default_value( false )
317 .help( UTF8STDSTR( _( "prints version information and exits" ) ) )
318 .implicit_value( true )
319 .nargs( 0 );
320
321 argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
322 .default_value( false )
323 .help( UTF8STDSTR( ARG_HELP_DESC ) )
324 .implicit_value( true )
325 .nargs( 0 );
326
327 for( COMMAND_ENTRY& entry : commandStack )
328 {
329 recurseArgParserBuild( argParser, entry );
330 }
331
332 try
333 {
334 // Use the C locale to parse arguments
335 // Otherwise the decimal separator for the locale will be applied
337 argParser.parse_args( m_argcUtf8, m_argvUtf8 );
338 }
339 // std::runtime_error doesn't seem to be enough for the scan<>()
340 catch( const std::exception& err )
341 {
342 wxPrintf( "%s\n", err.what() );
343
344 // find the correct argparser object to output the command usage info
345 COMMAND_ENTRY* cliCmd = nullptr;
346 for( COMMAND_ENTRY& entry : commandStack )
347 {
348 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
349 {
350 cliCmd = recurseArgParserSubCommandUsed( argParser, entry );
351 }
352 }
353
354 // arg parser uses a stream overload for printing the help
355 // we want to intercept so we can wxString the utf8 contents
356 // because on windows our terminal codepage might not be utf8
357 if( cliCmd )
358 cliCmd->handler->PrintHelp();
359 else
360 {
361 printHelp( argParser );
362 }
363
365 }
366
367 if( argParser[ ARG_HELP ] == true )
368 {
369 std::stringstream ss;
370 ss << argParser;
371 wxPrintf( From_UTF8( ss.str().c_str() ) );
372
373 return 0;
374 }
375
376 CLI::COMMAND* cliCmd = nullptr;
377
378 // the version arg gets redirected to the version subcommand
379 if( argParser[ARG_VERSION] == true )
380 {
381 cliCmd = &versionCmd;
382 }
383
384 if( !cliCmd )
385 {
386 for( COMMAND_ENTRY& entry : commandStack )
387 {
388 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
389 {
390 COMMAND_ENTRY* cmdSubEntry = recurseArgParserSubCommandUsed( argParser, entry );
391 if( cmdSubEntry != nullptr )
392 {
393 cliCmd = cmdSubEntry->handler;
394 break;
395 }
396 }
397 }
398 }
399
400 if( cliCmd )
401 {
402 int exitCode = cliCmd->Perform( Kiway );
403
404 if( exitCode != CLI::EXIT_CODES::AVOID_CLOSING )
405 {
406 return exitCode;
407 }
408 else
409 {
410 return 0;
411 }
412 }
413 else
414 {
415 printHelp( argParser );
416
418 }
419}
420
421
423{
425
427 {
429 m_settings_manager->Save();
430 }
431
432 // Destroy everything in PGM_KICAD,
433 // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
434 // than static destruction would.
435 Destroy();
436}
437
438
439void PGM_KICAD::MacOpenFile( const wxString& aFileName )
440{
441#if defined( __WXMAC__ )
442 wxFAIL_MSG( "kicad-cli should not call MacOpenFile" );
443#endif
444}
445
446
448{
449 // unlike a normal destructor, this is designed to be called more
450 // than once safely:
451
452 m_bm.End();
453
455}
456
457
459
460
464struct APP_KICAD_CLI : public wxAppConsole
465{
466 APP_KICAD_CLI() : wxAppConsole()
467 {
468 // Init the environment each platform wants
470 }
471
472
473 bool OnInit() override
474 {
475 // Perform platform-specific init tasks
476 if( !KIPLATFORM::APP::Init() )
477 return false;
478
479 if( !program.OnPgmInit() )
480 {
482 return false;
483 }
484
485 return true;
486 }
487
488 int OnExit() override
489 {
491
492#if defined( __FreeBSD__ )
493 // Avoid wxLog crashing when used in destructors.
494 wxLog::EnableLogging( false );
495#endif
496
497 return wxAppConsole::OnExit();
498 }
499
500 int OnRun() override
501 {
502 try
503 {
504 return program.OnPgmRun();
505 }
506 catch( ... )
507 {
508 Pgm().HandleException( std::current_exception() );
509 }
510
511 return -1;
512 }
513
514 int FilterEvent( wxEvent& aEvent ) override
515 {
516 return Event_Skip;
517 }
518
519#if defined( DEBUG )
523 bool ProcessEvent( wxEvent& aEvent ) override
524 {
525 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
526 {
527 wxKeyEvent* keyEvent = static_cast<wxKeyEvent*>( &aEvent );
528
529 if( keyEvent )
530 {
531 wxLogTrace( kicadTraceKeyEvent, "APP_KICAD::ProcessEvent %s", dump( *keyEvent ) );
532 }
533 }
534
535 aEvent.Skip();
536 return false;
537 }
538
546 bool OnExceptionInMainLoop() override
547 {
548 try
549 {
550 throw;
551 }
552 catch( ... )
553 {
554 Pgm().HandleException( std::current_exception() );
555 }
556
557 return false; // continue on. Return false to abort program
558 }
559#endif
560};
561
562IMPLEMENT_APP_CONSOLE( APP_KICAD_CLI )
563
564
565// The C++ project manager supports one open PROJECT, so Prj() calls within
566// this link image need this function.
568{
569 return Kiway.Prj();
570}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
argparse::ArgumentParser & GetArgParser()
Definition: command.h:59
const std::string & GetName() const
Definition: command.h:60
void PrintHelp()
Definition: command.cpp:48
int Perform(KIWAY &aKiway)
Entry point to processing commands from args and doing work.
Definition: command.cpp:56
A KIFACE implementation.
Definition: kiface_base.h:39
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:279
void OnKiwayEnd()
Definition: kiway.cpp:752
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:196
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
Container for data for KiCad programs.
Definition: pgm_base.h:99
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:173
int m_argcUtf8
argv parameters converted to utf8 form, because wxwidgets has opinions and will return argv as either...
Definition: pgm_base.h:426
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition: pgm_base.h:397
void Destroy()
Definition: pgm_base.cpp:157
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:441
char ** m_argvUtf8
Definition: pgm_base.h:423
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition: pgm_base.cpp:403
void HandleException(std::exception_ptr aPtr)
A exception handler to be used at the top level if exceptions bubble up that for.
Definition: pgm_base.cpp:890
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:139
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition: pgm_base.cpp:625
PGM_KICAD extends PGM_BASE to bring in FileHistory() and PdfBrowser() which were moved from EDA_APP i...
Definition: pgm_kicad.h:38
bool OnPgmInit()
Definition: kicad.cpp:95
void Destroy()
Definition: kicad.cpp:402
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:389
void OnPgmExit()
Definition: kicad.cpp:371
APP_SETTINGS_BASE * PgmSettings()
Definition: pgm_kicad.h:55
int OnPgmRun()
Definition: kicad.cpp:365
BIN_MOD m_bm
Definition: pgm_kicad.h:68
Container for project specific data.
Definition: project.h:62
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
#define ARG_HELP
Definition: command.h:30
#define UTF8STDSTR(s)
Definition: command.h:27
#define ARG_HELP_DESC
Definition: command.h:32
#define ARG_VERSION
Definition: command.h:29
#define ARG_HELP_SHORT
Definition: command.h:31
#define _(s)
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
static CLI::PCB_EXPORT_SVG_COMMAND exportPcbSvgCmd
Definition: kicad_cli.cpp:135
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchHpglCmd
Definition: kicad_cli.cpp:148
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchSvgCmd
Definition: kicad_cli.cpp:151
static CLI::PCB_EXPORT_3D_COMMAND exportPcbVrmlCmd
Definition: kicad_cli.cpp:134
PGM_KICAD & PgmTop()
Definition: kicad_cli.cpp:111
static CLI::PCB_EXPORT_DXF_COMMAND exportPcbDxfCmd
Definition: kicad_cli.cpp:131
static CLI::SCH_ERC_COMMAND schErcCmd
Definition: kicad_cli.cpp:143
static CLI::PCB_DRC_COMMAND pcbDrcCmd
Definition: kicad_cli.cpp:129
static CLI::PCB_EXPORT_GERBER_COMMAND exportPcbGerberCmd
Definition: kicad_cli.cpp:138
static CLI::FP_EXPORT_SVG_COMMAND fpExportSvgCmd
Definition: kicad_cli.cpp:154
static CLI::PCB_EXPORT_COMMAND exportPcbCmd
Definition: kicad_cli.cpp:140
static CLI::SYM_UPGRADE_COMMAND symUpgradeCmd
Definition: kicad_cli.cpp:159
static CLI::FP_EXPORT_COMMAND fpExportCmd
Definition: kicad_cli.cpp:153
static CLI::SCH_EXPORT_PYTHONBOM_COMMAND exportSchPythonBomCmd
Definition: kicad_cli.cpp:145
static CLI::FP_UPGRADE_COMMAND fpUpgradeCmd
Definition: kicad_cli.cpp:155
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPdfCmd
Definition: kicad_cli.cpp:149
static void printHelp(argparse::ArgumentParser &argParser)
Definition: kicad_cli.cpp:275
static CLI::PCB_EXPORT_POS_COMMAND exportPcbPosCmd
Definition: kicad_cli.cpp:137
static CLI::SYM_COMMAND symCmd
Definition: kicad_cli.cpp:156
PROJECT & Prj()
Definition: kicad_cli.cpp:567
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStepCmd
Definition: kicad_cli.cpp:133
static CLI::PCB_EXPORT_GERBERS_COMMAND exportPcbGerbersCmd
Definition: kicad_cli.cpp:139
static PGM_KICAD program
Definition: kicad_cli.cpp:95
static CLI::SCH_EXPORT_BOM_COMMAND exportSchBomCmd
Definition: kicad_cli.cpp:144
static CLI::SCH_COMMAND schCmd
Definition: kicad_cli.cpp:142
static CLI::PCB_COMMAND pcbCmd
Definition: kicad_cli.cpp:128
static std::vector< COMMAND_ENTRY > commandStack
Definition: kicad_cli.cpp:163
static CLI::PCB_EXPORT_DRILL_COMMAND exportPcbDrillCmd
Definition: kicad_cli.cpp:130
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchDxfCmd
Definition: kicad_cli.cpp:147
static CLI::SYM_EXPORT_COMMAND symExportCmd
Definition: kicad_cli.cpp:157
static CLI::SYM_EXPORT_SVG_COMMAND symExportSvgCmd
Definition: kicad_cli.cpp:158
static COMMAND_ENTRY * recurseArgParserSubCommandUsed(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:253
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad_cli.cpp:98
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: kicad_cli.cpp:84
static CLI::VERSION_COMMAND versionCmd
Definition: kicad_cli.cpp:160
static CLI::PCB_EXPORT_PDF_COMMAND exportPcbPdfCmd
Definition: kicad_cli.cpp:136
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: kicad_cli.cpp:105
static void recurseArgParserBuild(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:242
static CLI::SCH_EXPORT_NETLIST_COMMAND exportSchNetlistCmd
Definition: kicad_cli.cpp:146
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPostscriptCmd
Definition: kicad_cli.cpp:150
static CLI::FP_COMMAND fpCmd
Definition: kicad_cli.cpp:152
static CLI::PCB_EXPORT_3D_COMMAND exportPcbGlbCmd
Definition: kicad_cli.cpp:132
static CLI::SCH_EXPORT_COMMAND exportSchCmd
Definition: kicad_cli.cpp:141
#define KFCTL_CPP_PROJECT_SUITE
Running under C++ project mgr, possibly with others.
Definition: kiway.h:159
KIWAY Kiway
#define KFCTL_CLI
Running as CLI app.
Definition: kiway.h:160
static const int ERR_ARGS
Definition: exit_codes.h:31
static const int AVOID_CLOSING
Definition: exit_codes.h:28
bool Init()
Perform application-specific initialization tasks.
Definition: gtk/app.cpp:40
void Init()
Perform environment initialization tasks.
std::vector< FAB_LAYER_COLOR > dummy
wxString From_UTF8(const char *cstring)
Not publicly visible because most of the action is in PGM_KICAD these days.
Definition: kicad_cli.cpp:465
int OnExit() override
Definition: kicad_cli.cpp:488
bool OnInit() override
Definition: kicad_cli.cpp:473
int FilterEvent(wxEvent &aEvent) override
Definition: kicad_cli.cpp:514
int OnRun() override
Definition: kicad_cli.cpp:500
void End()
Definition: bin_mod.cpp:50
void Init()
Definition: bin_mod.cpp:38
void InitSettings(APP_SETTINGS_BASE *aPtr)
Takes ownership of a new application settings object.
Definition: bin_mod.h:53
Definition: kicad_cli.cpp:118
COMMAND_ENTRY(CLI::COMMAND *aHandler)
Definition: kicad_cli.cpp:123
COMMAND_ENTRY(CLI::COMMAND *aHandler, std::vector< COMMAND_ENTRY > aSub)
Definition: kicad_cli.cpp:124
CLI::COMMAND * handler
Definition: kicad_cli.cpp:119
std::vector< COMMAND_ENTRY > subCommands
Definition: kicad_cli.cpp:121
System directories search utilities.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.