KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_diff_base.h
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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef KICAD_CLI_COMMAND_DIFF_BASE_H
25#define KICAD_CLI_COMMAND_DIFF_BASE_H
26
27#include "command.h"
28
30#include <cli/exit_codes.h>
31
32#include <kiway.h>
33#include <string_utils.h>
34
35#include <memory>
36#include <string>
37
38#include <wx/crt.h>
39
40
41namespace CLI
42{
43
52template <typename JOB_T>
53class DIFF_COMMAND : public COMMAND
54{
55public:
56 DIFF_COMMAND( const wxString& aDescription, const std::string& aArgA, const wxString& aHelpA,
57 const std::string& aMetaA, const std::string& aArgB, const wxString& aHelpB,
58 const std::string& aMetaB, KIWAY::FACE_T aFace ) :
59 COMMAND( "diff" ),
60 m_argA( aArgA ),
61 m_argB( aArgB ),
62 m_face( aFace )
63 {
64 m_argParser.add_description( UTF8STDSTR( aDescription ) );
65
66 m_argParser.add_argument( m_argA ).help( UTF8STDSTR( aHelpA ) ).metavar( aMetaA );
67 m_argParser.add_argument( m_argB ).help( UTF8STDSTR( aHelpB ) ).metavar( aMetaB );
68
69 m_argParser.add_argument( "--format" )
70 .default_value( std::string( "json" ) )
71 .help( UTF8STDSTR( _( "Output format: json, text, png, or svg (default: json)" ) ) )
72 .metavar( "FORMAT" );
73
74 m_argParser.add_argument( "-o", "--output" )
75 .default_value( std::string( "" ) )
76 .help( UTF8STDSTR( _( "Output file path; required for png/svg, otherwise prints to "
77 "stdout if omitted" ) ) )
78 .metavar( "PATH" );
79
80 m_argParser.add_argument( "--exit-code-only" )
81 .help( UTF8STDSTR( _( "Only set exit code (0=identical, 5=differences found, "
82 "non-zero on error), no output" ) ) )
83 .flag();
84 }
85
86protected:
87 int doPerform( KIWAY& aKiway ) override
88 {
89 std::unique_ptr<JOB_T> job = std::make_unique<JOB_T>();
90
91 job->m_inputA = From_UTF8( m_argParser.get<std::string>( m_argA ).c_str() );
92 job->m_inputB = From_UTF8( m_argParser.get<std::string>( m_argB ).c_str() );
93 job->m_exitCodeOnly = m_argParser.get<bool>( "--exit-code-only" );
94
95 job->SetConfiguredOutputPath( From_UTF8( m_argParser.get<std::string>( "--output" ).c_str() ) );
96
97 std::string fmt = m_argParser.get<std::string>( "--format" );
98
99 if( !ParseDiffOutputFormat( fmt, job->m_format ) )
100 {
103 }
104
105 if( DiffOutputFormatRequiresOutputPath( job->m_format )
106 && job->GetConfiguredOutputPath().IsEmpty() )
107 {
108 wxFprintf( stderr, _( "--output is required for png/svg formats\n" ) );
110 }
111
112 return aKiway.ProcessJob( m_face, job.get() );
113 }
114
115private:
116 std::string m_argA;
117 std::string m_argB;
119};
120
121} // namespace CLI
122
123#endif // KICAD_CLI_COMMAND_DIFF_BASE_H
argparse::ArgumentParser m_argParser
Definition command.h:113
COMMAND(const std::string &aName)
Define a new COMMAND instance.
Definition command.cpp:30
DIFF_COMMAND(const wxString &aDescription, const std::string &aArgA, const wxString &aHelpA, const std::string &aMetaA, const std::string &aArgB, const wxString &aHelpB, const std::string &aMetaB, KIWAY::FACE_T aFace)
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:311
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Definition kiway.cpp:746
FACE_T
Known KIFACE implementations.
Definition kiway.h:317
#define UTF8STDSTR(s)
Definition command.h:27
#define _(s)
static const int ERR_ARGS
Definition exit_codes.h:31
bool DiffOutputFormatRequiresOutputPath(Format aFormat)
void ReportInvalidDiffOutputFormat(const std::string &aText)
bool ParseDiffOutputFormat(const std::string &aText, Format &aOut)
wxString From_UTF8(const char *cstring)