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-2024 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_jobset.h"
51#include "cli/command_pcb.h"
53#include "cli/command_pcb_drc.h"
70#include "cli/command_fp.h"
74#include "cli/command_sch.h"
75#include "cli/command_sch_erc.h"
77#include "cli/command_sym.h"
81#include "cli/command_version.h"
82#include "cli/exit_codes.h"
83
84// Add this header after all others, to avoid a collision name in a Windows header
85// on mingw.
86#include <wx/app.h>
87
88// a dummy to quiet linking with EDA_BASE_FRAME::config();
89#include <kiface_base.h>
91{
92 // This function should never be called. It is only referenced from
93 // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
94 // not to be actually called.
95 wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
96
97 throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
98}
99
100
102{
104
105 std::vector<COMMAND_ENTRY> subCommands;
106
107 COMMAND_ENTRY( CLI::COMMAND* aHandler ) : handler( aHandler ){};
108 COMMAND_ENTRY( CLI::COMMAND* aHandler, std::vector<COMMAND_ENTRY> aSub ) :
109 handler( aHandler ), subCommands( aSub ){};
110};
111
155
156
157static std::vector<COMMAND_ENTRY> commandStack = {
158 {
159 &jobsetCmd,
160 {
161 {
163 }
164 }
165 },
166 {
167 &fpCmd,
168 {
169 {
171 {
173 }
174 },
175 {
177 }
178 }
179 },
180 {
181 &pcbCmd,
182 {
183 {
184 &pcbDrcCmd
185 },
186 {
188 },
189 {
191 {
209 }
210 }
211 }
212 },
213 {
214 &schCmd,
215 {
216 {
217 &schErcCmd
218 },
219 {
221 {
230 }
231 }
232 }
233 },
234 {
235 &symCmd,
236 {
237 {
239 {
241 }
242 },
243 {
245 }
246 }
247 },
248 {
249 &versionCmd,
250 }
251};
252
253
254static void recurseArgParserBuild( argparse::ArgumentParser& aArgParser, COMMAND_ENTRY& aEntry )
255{
256 aArgParser.add_subparser( aEntry.handler->GetArgParser() );
257
258 for( COMMAND_ENTRY& subEntry : aEntry.subCommands )
259 {
260 recurseArgParserBuild( aEntry.handler->GetArgParser(), subEntry );
261 }
262}
263
264
265static COMMAND_ENTRY* recurseArgParserSubCommandUsed( argparse::ArgumentParser& aArgParser,
266 COMMAND_ENTRY& aEntry )
267{
268 COMMAND_ENTRY* cliCmd = nullptr;
269
270 if( aArgParser.is_subcommand_used( aEntry.handler->GetName() ) )
271 {
272 for( COMMAND_ENTRY& subentry : aEntry.subCommands )
273 {
274 cliCmd = recurseArgParserSubCommandUsed( aEntry.handler->GetArgParser(), subentry );
275 if( cliCmd )
276 break;
277 }
278
279 if(!cliCmd)
280 cliCmd = &aEntry;
281 }
282
283 return cliCmd;
284}
285
286
287static void printHelp( argparse::ArgumentParser& argParser )
288{
289 std::stringstream ss;
290 ss << argParser;
291 wxPrintf( From_UTF8( ss.str().c_str() ) );
292}
293
294
296{
298 App().SetAppDisplayName( wxT( "kicad-cli" ) );
299
300#if defined( DEBUG )
301 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
302
303 if( !wxIsAbsolutePath( absoluteArgv0 ) )
304 {
305 wxLogError( wxT( "No meaningful argv[0]" ) );
306 return false;
307 }
308#endif
309
310 if( !InitPgm( true, true) )
311 return false;
312
316 m_bm.Init();
317
318 return true;
319}
320
321
323{
324 argparse::ArgumentParser argParser( std::string( "kicad-cli" ), GetMajorMinorVersion().ToStdString(),
325 argparse::default_arguments::none );
326
327 argParser.add_argument( "-v", ARG_VERSION )
328 .default_value( false )
329 .help( UTF8STDSTR( _( "prints version information and exits" ) ) )
330 .implicit_value( true )
331 .nargs( 0 );
332
333 argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
334 .default_value( false )
335 .help( UTF8STDSTR( ARG_HELP_DESC ) )
336 .implicit_value( true )
337 .nargs( 0 );
338
339 for( COMMAND_ENTRY& entry : commandStack )
340 {
341 recurseArgParserBuild( argParser, entry );
342 }
343
344 try
345 {
346 // Use the C locale to parse arguments
347 // Otherwise the decimal separator for the locale will be applied
349 argParser.parse_args( m_argcUtf8, m_argvUtf8 );
350 }
351 // std::runtime_error doesn't seem to be enough for the scan<>()
352 catch( const std::exception& err )
353 {
354 wxPrintf( "%s\n", err.what() );
355
356 // find the correct argparser object to output the command usage info
357 COMMAND_ENTRY* cliCmd = nullptr;
358 for( COMMAND_ENTRY& entry : commandStack )
359 {
360 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
361 {
362 cliCmd = recurseArgParserSubCommandUsed( argParser, entry );
363 }
364 }
365
366 // arg parser uses a stream overload for printing the help
367 // we want to intercept so we can wxString the utf8 contents
368 // because on windows our terminal codepage might not be utf8
369 if( cliCmd )
370 cliCmd->handler->PrintHelp();
371 else
372 {
373 printHelp( argParser );
374 }
375
377 }
378
379 if( argParser[ ARG_HELP ] == true )
380 {
381 std::stringstream ss;
382 ss << argParser;
383 wxPrintf( From_UTF8( ss.str().c_str() ) );
384
385 return 0;
386 }
387
388 CLI::COMMAND* cliCmd = nullptr;
389
390 // the version arg gets redirected to the version subcommand
391 if( argParser[ARG_VERSION] == true )
392 {
393 cliCmd = &versionCmd;
394 }
395
396 if( !cliCmd )
397 {
398 for( COMMAND_ENTRY& entry : commandStack )
399 {
400 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
401 {
402 COMMAND_ENTRY* cmdSubEntry = recurseArgParserSubCommandUsed( argParser, entry );
403 if( cmdSubEntry != nullptr )
404 {
405 cliCmd = cmdSubEntry->handler;
406 break;
407 }
408 }
409 }
410 }
411
412 if( cliCmd )
413 {
414 int exitCode = cliCmd->Perform( Kiway );
415
416 if( exitCode != CLI::EXIT_CODES::AVOID_CLOSING )
417 {
418 return exitCode;
419 }
420 else
421 {
422 return 0;
423 }
424 }
425 else
426 {
427 printHelp( argParser );
428
430 }
431}
432
433
435{
437
439 {
441 m_settings_manager->Save();
442 }
443
444 // Destroy everything in PGM_KICAD,
445 // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
446 // than static destruction would.
447 Destroy();
448}
449
450
451void PGM_KICAD::MacOpenFile( const wxString& aFileName )
452{
453#if defined( __WXMAC__ )
454 wxFAIL_MSG( "kicad-cli should not call MacOpenFile" );
455#endif
456}
457
458
460{
461 // unlike a normal destructor, this is designed to be called more
462 // than once safely:
463
464 m_bm.End();
465
467}
468
469
471
473
477struct APP_KICAD_CLI : public wxAppConsole
478{
479 APP_KICAD_CLI() : wxAppConsole()
480 {
481 SetPgm( &program );
482
483 // Init the environment each platform wants
485 }
486
487
488 bool OnInit() override
489 {
490 // Perform platform-specific init tasks
491 if( !KIPLATFORM::APP::Init() )
492 return false;
493
494#ifndef DEBUG
495 // Enable logging traces to the console in release build.
496 // This is usually disabled, but it can be useful for users to run to help
497 // debug issues and other problems.
498 if( wxGetEnv( wxS( "KICAD_ENABLE_WXTRACE" ), nullptr ) )
499 {
500 wxLog::EnableLogging( true );
501 wxLog::SetLogLevel( wxLOG_Trace );
502 }
503#endif
504
505 if( !program.OnPgmInit() )
506 {
508 return false;
509 }
510
511 return true;
512 }
513
514 int OnExit() override
515 {
517
518#if defined( __FreeBSD__ )
519 // Avoid wxLog crashing when used in destructors.
520 wxLog::EnableLogging( false );
521#endif
522
523 return wxAppConsole::OnExit();
524 }
525
526 int OnRun() override
527 {
528 try
529 {
530 return program.OnPgmRun();
531 }
532 catch( ... )
533 {
534 Pgm().HandleException( std::current_exception() );
535 }
536
537 return -1;
538 }
539
540 int FilterEvent( wxEvent& aEvent ) override
541 {
542 return Event_Skip;
543 }
544
545#if defined( DEBUG )
549 bool ProcessEvent( wxEvent& aEvent ) override
550 {
551 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
552 {
553 wxKeyEvent* keyEvent = static_cast<wxKeyEvent*>( &aEvent );
554
555 if( keyEvent )
556 {
557 wxLogTrace( kicadTraceKeyEvent, "APP_KICAD::ProcessEvent %s", dump( *keyEvent ) );
558 }
559 }
560
561 aEvent.Skip();
562 return false;
563 }
564
572 bool OnExceptionInMainLoop() override
573 {
574 try
575 {
576 throw;
577 }
578 catch( ... )
579 {
580 Pgm().HandleException( std::current_exception() );
581 }
582
583 return false; // continue on. Return false to abort program
584 }
585#endif
586};
587
588IMPLEMENT_APP_CONSOLE( APP_KICAD_CLI )
589
590
591// The C++ project manager supports one open PROJECT, so Prj() calls within
592// this link image need this function.
594{
595 return Kiway.Prj();
596}
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:284
void OnKiwayEnd()
Definition: kiway.cpp:740
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
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:182
int m_argcUtf8
argv parameters converted to utf8 form, because wxwidgets has opinions and will return argv as either...
Definition: pgm_base.h:441
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition: pgm_base.h:408
void Destroy()
Definition: pgm_base.cpp:170
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:457
char ** m_argvUtf8
Definition: pgm_base.h:438
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition: pgm_base.cpp:412
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:939
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition: pgm_base.cpp:670
PGM_KICAD extends PGM_BASE to bring in FileHistory() and PdfBrowser() which were moved from EDA_APP i...
Definition: pgm_kicad.h:42
bool OnPgmInit()
Definition: kicad.cpp:86
void Destroy()
Definition: kicad.cpp:410
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:397
void OnPgmExit()
Definition: kicad.cpp:375
APP_SETTINGS_BASE * PgmSettings()
Definition: pgm_kicad.h:59
int OnPgmRun()
Definition: kicad.cpp:369
BIN_MOD m_bm
Definition: pgm_kicad.h:72
Container for project specific data.
Definition: project.h:64
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:126
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchHpglCmd
Definition: kicad_cli.cpp:142
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchSvgCmd
Definition: kicad_cli.cpp:145
static CLI::PCB_EXPORT_3D_COMMAND exportPcbVrmlCmd
Definition: kicad_cli.cpp:123
static CLI::PCB_EXPORT_DXF_COMMAND exportPcbDxfCmd
Definition: kicad_cli.cpp:118
static CLI::SCH_ERC_COMMAND schErcCmd
Definition: kicad_cli.cpp:137
static CLI::PCB_DRC_COMMAND pcbDrcCmd
Definition: kicad_cli.cpp:115
static CLI::PCB_EXPORT_GERBER_COMMAND exportPcbGerberCmd
Definition: kicad_cli.cpp:129
static CLI::FP_EXPORT_SVG_COMMAND fpExportSvgCmd
Definition: kicad_cli.cpp:148
static CLI::PCB_EXPORT_COMMAND exportPcbCmd
Definition: kicad_cli.cpp:134
static CLI::PCB_RENDER_COMMAND pcbRenderCmd
Definition: kicad_cli.cpp:116
static CLI::SYM_UPGRADE_COMMAND symUpgradeCmd
Definition: kicad_cli.cpp:153
static CLI::FP_EXPORT_COMMAND fpExportCmd
Definition: kicad_cli.cpp:147
static CLI::SCH_EXPORT_PYTHONBOM_COMMAND exportSchPythonBomCmd
Definition: kicad_cli.cpp:139
static CLI::FP_UPGRADE_COMMAND fpUpgradeCmd
Definition: kicad_cli.cpp:149
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPdfCmd
Definition: kicad_cli.cpp:143
static void printHelp(argparse::ArgumentParser &argParser)
Definition: kicad_cli.cpp:287
static CLI::JOBSET_RUN_COMMAND jobsetRunCmd
Definition: kicad_cli.cpp:113
static CLI::PCB_EXPORT_POS_COMMAND exportPcbPosCmd
Definition: kicad_cli.cpp:128
static CLI::SYM_COMMAND symCmd
Definition: kicad_cli.cpp:150
PROJECT & Prj()
Definition: kicad_cli.cpp:593
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStepCmd
Definition: kicad_cli.cpp:120
static CLI::PCB_EXPORT_3D_COMMAND exportPcbPlyCmd
Definition: kicad_cli.cpp:124
static CLI::PCB_EXPORT_GERBERS_COMMAND exportPcbGerbersCmd
Definition: kicad_cli.cpp:130
static PGM_KICAD program
Definition: kicad_cli.cpp:472
static CLI::SCH_EXPORT_BOM_COMMAND exportSchBomCmd
Definition: kicad_cli.cpp:138
static CLI::SCH_COMMAND schCmd
Definition: kicad_cli.cpp:136
static CLI::PCB_COMMAND pcbCmd
Definition: kicad_cli.cpp:114
static std::vector< COMMAND_ENTRY > commandStack
Definition: kicad_cli.cpp:157
static CLI::PCB_EXPORT_DRILL_COMMAND exportPcbDrillCmd
Definition: kicad_cli.cpp:117
static CLI::PCB_EXPORT_IPC2581_COMMAND exportPcbIpc2581Cmd
Definition: kicad_cli.cpp:132
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchDxfCmd
Definition: kicad_cli.cpp:141
static CLI::JOBSET_COMMAND jobsetCmd
Definition: kicad_cli.cpp:112
static CLI::SYM_EXPORT_COMMAND symExportCmd
Definition: kicad_cli.cpp:151
static CLI::SYM_EXPORT_SVG_COMMAND symExportSvgCmd
Definition: kicad_cli.cpp:152
static COMMAND_ENTRY * recurseArgParserSubCommandUsed(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:265
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: kicad_cli.cpp:90
static CLI::PCB_EXPORT_ODB_COMMAND exportPcbOdbCmd
Definition: kicad_cli.cpp:133
static CLI::VERSION_COMMAND versionCmd
Definition: kicad_cli.cpp:154
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStlCmd
Definition: kicad_cli.cpp:125
static CLI::PCB_EXPORT_PDF_COMMAND exportPcbPdfCmd
Definition: kicad_cli.cpp:127
static CLI::PCB_EXPORT_3D_COMMAND exportPcbXaoCmd
Definition: kicad_cli.cpp:122
static void recurseArgParserBuild(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:254
static CLI::SCH_EXPORT_NETLIST_COMMAND exportSchNetlistCmd
Definition: kicad_cli.cpp:140
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPostscriptCmd
Definition: kicad_cli.cpp:144
static CLI::PCB_EXPORT_GENCAD_COMMAND exportPcbGencadCmd
Definition: kicad_cli.cpp:131
static CLI::FP_COMMAND fpCmd
Definition: kicad_cli.cpp:146
static CLI::PCB_EXPORT_3D_COMMAND exportPcbGlbCmd
Definition: kicad_cli.cpp:119
static CLI::SCH_EXPORT_COMMAND exportSchCmd
Definition: kicad_cli.cpp:135
static CLI::PCB_EXPORT_3D_COMMAND exportPcbBrepCmd
Definition: kicad_cli.cpp:121
#define KFCTL_CPP_PROJECT_SUITE
Running under C++ project mgr, possibly with others.
Definition: kiway.h:159
#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: unix/app.cpp:40
void Init()
Perform environment initialization tasks.
void SetPgm(PGM_BASE *pgm)
Definition: pgm_base.cpp:1072
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
KIWAY Kiway(KFCTL_STANDALONE)
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:478
int OnExit() override
Definition: kicad_cli.cpp:514
bool OnInit() override
Definition: kicad_cli.cpp:488
int FilterEvent(wxEvent &aEvent) override
Definition: kicad_cli.cpp:540
int OnRun() override
Definition: kicad_cli.cpp:526
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:102
COMMAND_ENTRY(CLI::COMMAND *aHandler)
Definition: kicad_cli.cpp:107
COMMAND_ENTRY(CLI::COMMAND *aHandler, std::vector< COMMAND_ENTRY > aSub)
Definition: kicad_cli.cpp:108
CLI::COMMAND * handler
Definition: kicad_cli.cpp:103
std::vector< COMMAND_ENTRY > subCommands
Definition: kicad_cli.cpp:105
System directories search utilities.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.