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