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>
33#include <string_utils.h>
34#include <paths.h>
37#include <systemdirsappend.h>
38#include <trace_helpers.h>
39
40#include <stdexcept>
41
42#include "pgm_kicad.h"
43#include "kicad_manager_frame.h"
44
45#include <build_version.h>
46#include <kiplatform/app.h>
48#include <locale_io.h>
49
50#include "cli/command_jobset.h"
52#include "cli/command_pcb.h"
54#include "cli/command_pcb_drc.h"
76#include "cli/command_fp.h"
80#include "cli/command_sch.h"
81#include "cli/command_sch_erc.h"
84#include "cli/command_sym.h"
88#include "cli/command_version.h"
89#include "cli/exit_codes.h"
90
91// Add this header after all others, to avoid a collision name in a Windows header
92// on mingw.
93#include <wx/app.h>
94
95// a dummy to quiet linking with EDA_BASE_FRAME::config();
96#include <kiface_base.h>
98{
99 // This function should never be called. It is only referenced from
100 // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
101 // not to be actually called.
102 wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
103
104 throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
105}
106
107
109{
111
112 std::vector<COMMAND_ENTRY> subCommands;
113
114 COMMAND_ENTRY( CLI::COMMAND* aHandler ) : handler( aHandler ){};
115 COMMAND_ENTRY( CLI::COMMAND* aHandler, std::vector<COMMAND_ENTRY> aSub ) :
116 handler( aHandler ), subCommands( aSub ){};
117};
118
172
173
174// clang-format off
175static std::vector<COMMAND_ENTRY> commandStack = {
176 {
177 &jobsetCmd,
178 {
179 {
181 }
182 }
183 },
184 {
185 &fpCmd,
186 {
187 {
189 {
191 }
192 },
193 {
195 }
196 }
197 },
198 {
199 &pcbCmd,
200 {
201 {
202 &pcbDrcCmd
203 },
204 {
206 },
207 {
209 {
234 }
235 },
236 {
238 }
239 }
240 },
241 {
242 &schCmd,
243 {
244 {
245 &schErcCmd
246 },
247 {
249 {
258 }
259 },
260 {
262 }
263 }
264 },
265 {
266 &symCmd,
267 {
268 {
270 {
272 }
273 },
274 {
276 }
277 }
278 },
279 {
280 &versionCmd,
281 }
282};
283// clang-format on
284
285
286static void recurseArgParserBuild( argparse::ArgumentParser& aArgParser, COMMAND_ENTRY& aEntry )
287{
288 aArgParser.add_subparser( aEntry.handler->GetArgParser() );
289
290 for( COMMAND_ENTRY& subEntry : aEntry.subCommands )
291 {
292 recurseArgParserBuild( aEntry.handler->GetArgParser(), subEntry );
293 }
294}
295
296
297static COMMAND_ENTRY* recurseArgParserSubCommandUsed( argparse::ArgumentParser& aArgParser,
298 COMMAND_ENTRY& aEntry )
299{
300 COMMAND_ENTRY* cliCmd = nullptr;
301
302 if( aArgParser.is_subcommand_used( aEntry.handler->GetName() ) )
303 {
304 for( COMMAND_ENTRY& subentry : aEntry.subCommands )
305 {
306 cliCmd = recurseArgParserSubCommandUsed( aEntry.handler->GetArgParser(), subentry );
307 if( cliCmd )
308 break;
309 }
310
311 if(!cliCmd)
312 cliCmd = &aEntry;
313 }
314
315 return cliCmd;
316}
317
318
319static void printHelp( argparse::ArgumentParser& argParser )
320{
321 std::stringstream ss;
322 ss << argParser;
323 wxPrintf( From_UTF8( ss.str().c_str() ) );
324}
325
326
328{
330 App().SetAppDisplayName( wxT( "kicad-cli" ) );
331
332#if defined( DEBUG )
333 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
334
335 if( !wxIsAbsolutePath( absoluteArgv0 ) )
336 {
337 wxLogError( wxT( "No meaningful argv[0]" ) );
338 return false;
339 }
340#endif
341
342 if( !InitPgm( true, true) )
343 return false;
344
345 m_bm.InitSettings( new KICAD_SETTINGS );
348 m_bm.Init();
349
351
352 return true;
353}
354
355
357{
358 argparse::ArgumentParser argParser( std::string( "kicad-cli" ), GetMajorMinorVersion().ToStdString(),
359 argparse::default_arguments::none );
360
361 argParser.add_argument( "-v", ARG_VERSION )
362 .help( UTF8STDSTR( _( "prints version information and exits" ) ) )
363 .flag()
364 .nargs( 0 );
365
366 argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
367 .help( UTF8STDSTR( ARG_HELP_DESC ) )
368 .flag()
369 .nargs( 0 );
370
371 for( COMMAND_ENTRY& entry : commandStack )
372 {
373 recurseArgParserBuild( argParser, entry );
374 }
375
376 try
377 {
378 // Use the C locale to parse arguments
379 // Otherwise the decimal separator for the locale will be applied
380 LOCALE_IO dummy;
381 argParser.parse_args( m_argcUtf8, m_argvUtf8 );
382 }
383 // std::runtime_error doesn't seem to be enough for the scan<>()
384 catch( const std::exception& err )
385 {
386 bool requestedHelp = false;
387
388 for( int i = 0; i < m_argcUtf8; ++i )
389 {
390 if( std::string arg( m_argvUtf8[i] ); arg == ARG_HELP_SHORT || arg == ARG_HELP )
391 {
392 requestedHelp = true;
393 break;
394 }
395 }
396
397 if( !requestedHelp )
398 wxPrintf( "%s\n", err.what() );
399
400 // find the correct argparser object to output the command usage info
401 COMMAND_ENTRY* cliCmd = nullptr;
402 for( COMMAND_ENTRY& entry : commandStack )
403 {
404 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
405 {
406 cliCmd = recurseArgParserSubCommandUsed( argParser, entry );
407 }
408 }
409
410 // arg parser uses a stream overload for printing the help
411 // we want to intercept so we can wxString the utf8 contents
412 // because on windows our terminal codepage might not be utf8
413 if( cliCmd )
414 cliCmd->handler->PrintHelp();
415 else
416 {
417 printHelp( argParser );
418 }
419
420 return requestedHelp ? 0 : CLI::EXIT_CODES::ERR_ARGS;
421 }
422
423 if( argParser[ ARG_HELP ] == true )
424 {
425 std::stringstream ss;
426 ss << argParser;
427 wxPrintf( From_UTF8( ss.str().c_str() ) );
428
429 return 0;
430 }
431
432 CLI::COMMAND* cliCmd = nullptr;
433
434 // the version arg gets redirected to the version subcommand
435 if( argParser[ARG_VERSION] == true )
436 {
437 cliCmd = &versionCmd;
438 }
439
440 if( !cliCmd )
441 {
442 for( COMMAND_ENTRY& entry : commandStack )
443 {
444 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
445 {
446 COMMAND_ENTRY* cmdSubEntry = recurseArgParserSubCommandUsed( argParser, entry );
447 if( cmdSubEntry != nullptr )
448 {
449 cliCmd = cmdSubEntry->handler;
450 break;
451 }
452 }
453 }
454 }
455
456 if( cliCmd )
457 {
458 int exitCode = cliCmd->Perform( Kiway );
459
460 if( exitCode != CLI::EXIT_CODES::AVOID_CLOSING )
461 {
462 return exitCode;
463 }
464 else
465 {
466 return 0;
467 }
468 }
469 else
470 {
471 printHelp( argParser );
472
474 }
475}
476
477
479{
481
483 {
485 m_settings_manager->Save();
486 }
487
488 // Destroy everything in PGM_KICAD,
489 // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
490 // than static destruction would.
491 Destroy();
492}
493
494
495void PGM_KICAD::MacOpenFile( const wxString& aFileName )
496{
497#if defined( __WXMAC__ )
498 wxFAIL_MSG( "kicad-cli should not call MacOpenFile" );
499#endif
500}
501
502
504{
505 // unlike a normal destructor, this is designed to be called more
506 // than once safely:
507
508 m_bm.End();
509
511}
512
513
515
517
521struct APP_KICAD_CLI : public wxAppConsole
522{
523 APP_KICAD_CLI() : wxAppConsole()
524 {
525 SetPgm( &program );
526
527 // Init the environment each platform wants
529 }
530
531
532 bool OnInit() override
533 {
534 // Perform platform-specific init tasks
535 if( !KIPLATFORM::APP::Init() )
536 return false;
537
538#ifndef DEBUG
539 // Enable logging traces to the console in release build.
540 // This is usually disabled, but it can be useful for users to run to help
541 // debug issues and other problems.
542 if( wxGetEnv( wxS( "KICAD_ENABLE_WXTRACE" ), nullptr ) )
543 {
544 wxLog::EnableLogging( true );
545 wxLog::SetLogLevel( wxLOG_Trace );
546 }
547#endif
548
549 if( !program.OnPgmInit() )
550 {
551 program.OnPgmExit();
552 return false;
553 }
554
555 return true;
556 }
557
558 int OnExit() override
559 {
560 program.OnPgmExit();
561
562#if defined( __FreeBSD__ )
563 // Avoid wxLog crashing when used in destructors.
564 wxLog::EnableLogging( false );
565#endif
566
567 return wxAppConsole::OnExit();
568 }
569
570 int OnRun() override
571 {
572 try
573 {
574 return program.OnPgmRun();
575 }
576 catch( ... )
577 {
578 Pgm().HandleException( std::current_exception() );
579 }
580
581 return -1;
582 }
583
584 int FilterEvent( wxEvent& aEvent ) override
585 {
586 return Event_Skip;
587 }
588
589#if defined( DEBUG )
593 bool ProcessEvent( wxEvent& aEvent ) override
594 {
595 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
596 {
597 wxKeyEvent* keyEvent = static_cast<wxKeyEvent*>( &aEvent );
598
599 if( keyEvent )
600 {
601 wxLogTrace( kicadTraceKeyEvent, "APP_KICAD::ProcessEvent %s", dump( *keyEvent ) );
602 }
603 }
604
605 aEvent.Skip();
606 return false;
607 }
608
616 bool OnExceptionInMainLoop() override
617 {
618 try
619 {
620 throw;
621 }
622 catch( ... )
623 {
624 Pgm().HandleException( std::current_exception() );
625 }
626
627 return false; // continue on. Return false to abort program
628 }
629#endif
630};
631
632IMPLEMENT_APP_CONSOLE( APP_KICAD_CLI )
633
634
635// The C++ project manager supports one open PROJECT, so Prj() calls within
636// this link image need this function.
638{
639 return Kiway.Prj();
640}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
argparse::ArgumentParser & GetArgParser()
Definition command.h:60
const std::string & GetName() const
Definition command.h:61
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:292
void OnKiwayEnd()
Definition kiway.cpp:769
void LoadGlobalTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the global library tables in the given list, or all tables if no list is given
virtual wxApp & App()
Return a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition pgm_base.cpp:192
int m_argcUtf8
Definition pgm_base.h:426
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition pgm_base.h:386
void Destroy()
Definition pgm_base.cpp:178
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition pgm_base.cpp:308
char ** m_argvUtf8
argv parameters converted to utf8 form because wxWidgets has opinions.
Definition pgm_base.h:424
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition pgm_base.cpp:263
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:798
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:128
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:130
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition pgm_base.cpp:528
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:91
void Destroy()
Definition kicad.cpp:436
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
Definition kicad.cpp:423
void OnPgmExit()
Definition kicad.cpp:399
APP_SETTINGS_BASE * PgmSettings()
Definition pgm_kicad.h:59
int OnPgmRun()
Definition kicad.cpp:393
BIN_MOD m_bm
Definition pgm_kicad.h:72
Container for project specific data.
Definition project.h:66
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.
PROJECT & Prj()
Definition kicad.cpp:623
static CLI::PCB_EXPORT_SVG_COMMAND exportPcbSvgCmd
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchHpglCmd
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchSvgCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbVrmlCmd
static CLI::PCB_EXPORT_STATS_COMMAND exportPcbStatsCmd
static CLI::PCB_EXPORT_DXF_COMMAND exportPcbDxfCmd
static CLI::SCH_ERC_COMMAND schErcCmd
static CLI::PCB_EXPORT_HPGL_COMMAND exportPcbHpglCmd
static CLI::PCB_DRC_COMMAND pcbDrcCmd
static CLI::PCB_EXPORT_GERBER_COMMAND exportPcbGerberCmd
static CLI::FP_EXPORT_SVG_COMMAND fpExportSvgCmd
static CLI::PCB_EXPORT_COMMAND exportPcbCmd
static CLI::PCB_RENDER_COMMAND pcbRenderCmd
static CLI::SYM_UPGRADE_COMMAND symUpgradeCmd
static CLI::FP_EXPORT_COMMAND fpExportCmd
static CLI::SCH_EXPORT_PYTHONBOM_COMMAND exportSchPythonBomCmd
static CLI::FP_UPGRADE_COMMAND fpUpgradeCmd
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPdfCmd
static void printHelp(argparse::ArgumentParser &argParser)
static CLI::JOBSET_RUN_COMMAND jobsetRunCmd
static CLI::PCB_UPGRADE_COMMAND pcbUpgradeCmd
static CLI::PCB_EXPORT_POS_COMMAND exportPcbPosCmd
static CLI::PCB_EXPORT_PS_COMMAND exportPcbPsCmd
static CLI::SYM_COMMAND symCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStepCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbPlyCmd
static CLI::PCB_EXPORT_GERBERS_COMMAND exportPcbGerbersCmd
static PGM_KICAD program
static CLI::SCH_EXPORT_BOM_COMMAND exportSchBomCmd
static CLI::PCB_EXPORT_IPCD356_COMMAND exportPcbIpcD356Cmd
static CLI::SCH_COMMAND schCmd
static CLI::PCB_COMMAND pcbCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcb3DPDFCmd
static std::vector< COMMAND_ENTRY > commandStack
static CLI::PCB_EXPORT_DRILL_COMMAND exportPcbDrillCmd
static CLI::PCB_EXPORT_IPC2581_COMMAND exportPcbIpc2581Cmd
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchDxfCmd
static CLI::JOBSET_COMMAND jobsetCmd
static CLI::SYM_EXPORT_COMMAND symExportCmd
static CLI::SYM_EXPORT_SVG_COMMAND symExportSvgCmd
static COMMAND_ENTRY * recurseArgParserSubCommandUsed(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
static CLI::PCB_EXPORT_3D_COMMAND exportPcbU3DCmd
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition kicad_cli.cpp:97
static CLI::PCB_EXPORT_ODB_COMMAND exportPcbOdbCmd
static CLI::VERSION_COMMAND versionCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStlCmd
static CLI::PCB_EXPORT_PDF_COMMAND exportPcbPdfCmd
static CLI::SCH_UPGRADE_COMMAND schUpgradeCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbXaoCmd
static void recurseArgParserBuild(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
static CLI::SCH_EXPORT_NETLIST_COMMAND exportSchNetlistCmd
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPostscriptCmd
static CLI::PCB_EXPORT_GENCAD_COMMAND exportPcbGencadCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStepzCmd
static CLI::FP_COMMAND fpCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbGlbCmd
static CLI::SCH_EXPORT_COMMAND exportSchCmd
static CLI::PCB_EXPORT_3D_COMMAND exportPcbBrepCmd
#define KFCTL_CPP_PROJECT_SUITE
Running under C++ project mgr, possibly with others.
Definition kiway.h:163
#define KFCTL_CLI
Running as CLI app.
Definition kiway.h:164
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:960
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:946
PGM_SINGLE_TOP program
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.
int OnExit() override
bool OnInit() override
int FilterEvent(wxEvent &aEvent) override
int OnRun() override
COMMAND_ENTRY(CLI::COMMAND *aHandler)
COMMAND_ENTRY(CLI::COMMAND *aHandler, std::vector< COMMAND_ENTRY > aSub)
CLI::COMMAND * handler
std::vector< COMMAND_ENTRY > subCommands
System directories search utilities.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.