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_pcb.h"
51#include "cli/command_pcb_drc.h"
66#include "cli/command_fp.h"
70#include "cli/command_sch.h"
71#include "cli/command_sch_erc.h"
73#include "cli/command_sym.h"
77#include "cli/command_version.h"
78#include "cli/exit_codes.h"
79
80// Add this header after all others, to avoid a collision name in a Windows header
81// on mingw.
82#include <wx/app.h>
83
84// a dummy to quiet linking with EDA_BASE_FRAME::config();
85#include <kiface_base.h>
87{
88 // This function should never be called. It is only referenced from
89 // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
90 // not to be actually called.
91 wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
92
93 throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
94}
95
96
98{
100
101 std::vector<COMMAND_ENTRY> subCommands;
102
103 COMMAND_ENTRY( CLI::COMMAND* aHandler ) : handler( aHandler ){};
104 COMMAND_ENTRY( CLI::COMMAND* aHandler, std::vector<COMMAND_ENTRY> aSub ) :
105 handler( aHandler ), subCommands( aSub ){};
106};
107
144
145
146static std::vector<COMMAND_ENTRY> commandStack = {
147 {
148 &fpCmd,
149 {
150 {
152 {
154 }
155 },
156 {
158 }
159 }
160 },
161 {
162 &pcbCmd,
163 {
164 {
165 &pcbDrcCmd
166 },
167 {
169 },
170 {
172 {
185 }
186 }
187 }
188 },
189 {
190 &schCmd,
191 {
192 {
193 &schErcCmd
194 },
195 {
197 {
206 }
207 }
208 }
209 },
210 {
211 &symCmd,
212 {
213 {
215 {
217 }
218 },
219 {
221 }
222 }
223 },
224 {
225 &versionCmd,
226 }
227};
228
229
230static void recurseArgParserBuild( argparse::ArgumentParser& aArgParser, COMMAND_ENTRY& aEntry )
231{
232 aArgParser.add_subparser( aEntry.handler->GetArgParser() );
233
234 for( COMMAND_ENTRY& subEntry : aEntry.subCommands )
235 {
236 recurseArgParserBuild( aEntry.handler->GetArgParser(), subEntry );
237 }
238}
239
240
241static COMMAND_ENTRY* recurseArgParserSubCommandUsed( argparse::ArgumentParser& aArgParser,
242 COMMAND_ENTRY& aEntry )
243{
244 COMMAND_ENTRY* cliCmd = nullptr;
245
246 if( aArgParser.is_subcommand_used( aEntry.handler->GetName() ) )
247 {
248 for( COMMAND_ENTRY& subentry : aEntry.subCommands )
249 {
250 cliCmd = recurseArgParserSubCommandUsed( aEntry.handler->GetArgParser(), subentry );
251 if( cliCmd )
252 break;
253 }
254
255 if(!cliCmd)
256 cliCmd = &aEntry;
257 }
258
259 return cliCmd;
260}
261
262
263static void printHelp( argparse::ArgumentParser& argParser )
264{
265 std::stringstream ss;
266 ss << argParser;
267 wxPrintf( From_UTF8( ss.str().c_str() ) );
268}
269
270
272{
274 App().SetAppDisplayName( wxT( "kicad-cli" ) );
275
276#if defined( DEBUG )
277 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
278
279 if( !wxIsAbsolutePath( absoluteArgv0 ) )
280 {
281 wxLogError( wxT( "No meaningful argv[0]" ) );
282 return false;
283 }
284#endif
285
286 if( !InitPgm( true, true) )
287 return false;
288
292 m_bm.Init();
293
294 return true;
295}
296
297
299{
300 argparse::ArgumentParser argParser( std::string( "kicad-cli" ), GetMajorMinorVersion().ToStdString(),
301 argparse::default_arguments::none );
302
303 argParser.add_argument( "-v", ARG_VERSION )
304 .default_value( false )
305 .help( UTF8STDSTR( _( "prints version information and exits" ) ) )
306 .implicit_value( true )
307 .nargs( 0 );
308
309 argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
310 .default_value( false )
311 .help( UTF8STDSTR( ARG_HELP_DESC ) )
312 .implicit_value( true )
313 .nargs( 0 );
314
315 for( COMMAND_ENTRY& entry : commandStack )
316 {
317 recurseArgParserBuild( argParser, entry );
318 }
319
320 try
321 {
322 // Use the C locale to parse arguments
323 // Otherwise the decimal separator for the locale will be applied
325 argParser.parse_args( m_argcUtf8, m_argvUtf8 );
326 }
327 // std::runtime_error doesn't seem to be enough for the scan<>()
328 catch( const std::exception& err )
329 {
330 wxPrintf( "%s\n", err.what() );
331
332 // find the correct argparser object to output the command usage info
333 COMMAND_ENTRY* cliCmd = nullptr;
334 for( COMMAND_ENTRY& entry : commandStack )
335 {
336 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
337 {
338 cliCmd = recurseArgParserSubCommandUsed( argParser, entry );
339 }
340 }
341
342 // arg parser uses a stream overload for printing the help
343 // we want to intercept so we can wxString the utf8 contents
344 // because on windows our terminal codepage might not be utf8
345 if( cliCmd )
346 cliCmd->handler->PrintHelp();
347 else
348 {
349 printHelp( argParser );
350 }
351
353 }
354
355 if( argParser[ ARG_HELP ] == true )
356 {
357 std::stringstream ss;
358 ss << argParser;
359 wxPrintf( From_UTF8( ss.str().c_str() ) );
360
361 return 0;
362 }
363
364 CLI::COMMAND* cliCmd = nullptr;
365
366 // the version arg gets redirected to the version subcommand
367 if( argParser[ARG_VERSION] == true )
368 {
369 cliCmd = &versionCmd;
370 }
371
372 if( !cliCmd )
373 {
374 for( COMMAND_ENTRY& entry : commandStack )
375 {
376 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
377 {
378 COMMAND_ENTRY* cmdSubEntry = recurseArgParserSubCommandUsed( argParser, entry );
379 if( cmdSubEntry != nullptr )
380 {
381 cliCmd = cmdSubEntry->handler;
382 break;
383 }
384 }
385 }
386 }
387
388 if( cliCmd )
389 {
390 int exitCode = cliCmd->Perform( Kiway );
391
392 if( exitCode != CLI::EXIT_CODES::AVOID_CLOSING )
393 {
394 return exitCode;
395 }
396 else
397 {
398 return 0;
399 }
400 }
401 else
402 {
403 printHelp( argParser );
404
406 }
407}
408
409
411{
413
415 {
417 m_settings_manager->Save();
418 }
419
420 // Destroy everything in PGM_KICAD,
421 // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
422 // than static destruction would.
423 Destroy();
424}
425
426
427void PGM_KICAD::MacOpenFile( const wxString& aFileName )
428{
429#if defined( __WXMAC__ )
430 wxFAIL_MSG( "kicad-cli should not call MacOpenFile" );
431#endif
432}
433
434
436{
437 // unlike a normal destructor, this is designed to be called more
438 // than once safely:
439
440 m_bm.End();
441
443}
444
445
447
449
453struct APP_KICAD_CLI : public wxAppConsole
454{
455 APP_KICAD_CLI() : wxAppConsole()
456 {
457 SetPgm( &program );
458
459 // Init the environment each platform wants
461 }
462
463
464 bool OnInit() override
465 {
466 // Perform platform-specific init tasks
467 if( !KIPLATFORM::APP::Init() )
468 return false;
469
470#ifndef DEBUG
471 // Enable logging traces to the console in release build.
472 // This is usually disabled, but it can be useful for users to run to help
473 // debug issues and other problems.
474 if( wxGetEnv( wxS( "KICAD_ENABLE_WXTRACE" ), nullptr ) )
475 {
476 wxLog::EnableLogging( true );
477 wxLog::SetLogLevel( wxLOG_Trace );
478 }
479#endif
480
481 if( !program.OnPgmInit() )
482 {
484 return false;
485 }
486
487 return true;
488 }
489
490 int OnExit() override
491 {
493
494#if defined( __FreeBSD__ )
495 // Avoid wxLog crashing when used in destructors.
496 wxLog::EnableLogging( false );
497#endif
498
499 return wxAppConsole::OnExit();
500 }
501
502 int OnRun() override
503 {
504 try
505 {
506 return program.OnPgmRun();
507 }
508 catch( ... )
509 {
510 Pgm().HandleException( std::current_exception() );
511 }
512
513 return -1;
514 }
515
516 int FilterEvent( wxEvent& aEvent ) override
517 {
518 return Event_Skip;
519 }
520
521#if defined( DEBUG )
525 bool ProcessEvent( wxEvent& aEvent ) override
526 {
527 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
528 {
529 wxKeyEvent* keyEvent = static_cast<wxKeyEvent*>( &aEvent );
530
531 if( keyEvent )
532 {
533 wxLogTrace( kicadTraceKeyEvent, "APP_KICAD::ProcessEvent %s", dump( *keyEvent ) );
534 }
535 }
536
537 aEvent.Skip();
538 return false;
539 }
540
548 bool OnExceptionInMainLoop() override
549 {
550 try
551 {
552 throw;
553 }
554 catch( ... )
555 {
556 Pgm().HandleException( std::current_exception() );
557 }
558
559 return false; // continue on. Return false to abort program
560 }
561#endif
562};
563
564IMPLEMENT_APP_CONSOLE( APP_KICAD_CLI )
565
566
567// The C++ project manager supports one open PROJECT, so Prj() calls within
568// this link image need this function.
570{
571 return Kiway.Prj();
572}
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:732
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:181
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:165
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:456
char ** m_argvUtf8
Definition: pgm_base.h:438
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition: pgm_base.cpp:411
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:938
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:669
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:86
void Destroy()
Definition: kicad.cpp:408
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:395
void OnPgmExit()
Definition: kicad.cpp:373
APP_SETTINGS_BASE * PgmSettings()
Definition: pgm_kicad.h:55
int OnPgmRun()
Definition: kicad.cpp:367
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:117
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchHpglCmd
Definition: kicad_cli.cpp:131
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchSvgCmd
Definition: kicad_cli.cpp:134
static CLI::PCB_EXPORT_3D_COMMAND exportPcbVrmlCmd
Definition: kicad_cli.cpp:116
static CLI::PCB_EXPORT_DXF_COMMAND exportPcbDxfCmd
Definition: kicad_cli.cpp:112
static CLI::SCH_ERC_COMMAND schErcCmd
Definition: kicad_cli.cpp:126
static CLI::PCB_DRC_COMMAND pcbDrcCmd
Definition: kicad_cli.cpp:109
static CLI::PCB_EXPORT_GERBER_COMMAND exportPcbGerberCmd
Definition: kicad_cli.cpp:120
static CLI::FP_EXPORT_SVG_COMMAND fpExportSvgCmd
Definition: kicad_cli.cpp:137
static CLI::PCB_EXPORT_COMMAND exportPcbCmd
Definition: kicad_cli.cpp:123
static CLI::PCB_RENDER_COMMAND pcbRenderCmd
Definition: kicad_cli.cpp:110
static CLI::SYM_UPGRADE_COMMAND symUpgradeCmd
Definition: kicad_cli.cpp:142
static CLI::FP_EXPORT_COMMAND fpExportCmd
Definition: kicad_cli.cpp:136
static CLI::SCH_EXPORT_PYTHONBOM_COMMAND exportSchPythonBomCmd
Definition: kicad_cli.cpp:128
static CLI::FP_UPGRADE_COMMAND fpUpgradeCmd
Definition: kicad_cli.cpp:138
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPdfCmd
Definition: kicad_cli.cpp:132
static void printHelp(argparse::ArgumentParser &argParser)
Definition: kicad_cli.cpp:263
static CLI::PCB_EXPORT_POS_COMMAND exportPcbPosCmd
Definition: kicad_cli.cpp:119
static CLI::SYM_COMMAND symCmd
Definition: kicad_cli.cpp:139
PROJECT & Prj()
Definition: kicad_cli.cpp:569
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStepCmd
Definition: kicad_cli.cpp:114
static CLI::PCB_EXPORT_GERBERS_COMMAND exportPcbGerbersCmd
Definition: kicad_cli.cpp:121
static PGM_KICAD program
Definition: kicad_cli.cpp:448
static CLI::SCH_EXPORT_BOM_COMMAND exportSchBomCmd
Definition: kicad_cli.cpp:127
static CLI::SCH_COMMAND schCmd
Definition: kicad_cli.cpp:125
static CLI::PCB_COMMAND pcbCmd
Definition: kicad_cli.cpp:108
static std::vector< COMMAND_ENTRY > commandStack
Definition: kicad_cli.cpp:146
static CLI::PCB_EXPORT_DRILL_COMMAND exportPcbDrillCmd
Definition: kicad_cli.cpp:111
static CLI::PCB_EXPORT_IPC2581_COMMAND exportPcbIpc2581Cmd
Definition: kicad_cli.cpp:122
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchDxfCmd
Definition: kicad_cli.cpp:130
static CLI::SYM_EXPORT_COMMAND symExportCmd
Definition: kicad_cli.cpp:140
static CLI::SYM_EXPORT_SVG_COMMAND symExportSvgCmd
Definition: kicad_cli.cpp:141
static COMMAND_ENTRY * recurseArgParserSubCommandUsed(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:241
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: kicad_cli.cpp:86
static CLI::VERSION_COMMAND versionCmd
Definition: kicad_cli.cpp:143
static CLI::PCB_EXPORT_PDF_COMMAND exportPcbPdfCmd
Definition: kicad_cli.cpp:118
static void recurseArgParserBuild(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:230
static CLI::SCH_EXPORT_NETLIST_COMMAND exportSchNetlistCmd
Definition: kicad_cli.cpp:129
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPostscriptCmd
Definition: kicad_cli.cpp:133
static CLI::FP_COMMAND fpCmd
Definition: kicad_cli.cpp:135
static CLI::PCB_EXPORT_3D_COMMAND exportPcbGlbCmd
Definition: kicad_cli.cpp:113
static CLI::SCH_EXPORT_COMMAND exportSchCmd
Definition: kicad_cli.cpp:124
static CLI::PCB_EXPORT_3D_COMMAND exportPcbBrepCmd
Definition: kicad_cli.cpp:115
#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: gtk/app.cpp:40
void Init()
Perform environment initialization tasks.
void SetPgm(PGM_BASE *pgm)
Definition: pgm_base.cpp:1071
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
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:454
int OnExit() override
Definition: kicad_cli.cpp:490
bool OnInit() override
Definition: kicad_cli.cpp:464
int FilterEvent(wxEvent &aEvent) override
Definition: kicad_cli.cpp:516
int OnRun() override
Definition: kicad_cli.cpp:502
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:98
COMMAND_ENTRY(CLI::COMMAND *aHandler)
Definition: kicad_cli.cpp:103
COMMAND_ENTRY(CLI::COMMAND *aHandler, std::vector< COMMAND_ENTRY > aSub)
Definition: kicad_cli.cpp:104
CLI::COMMAND * handler
Definition: kicad_cli.cpp:99
std::vector< COMMAND_ENTRY > subCommands
Definition: kicad_cli.cpp:101
System directories search utilities.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.