From eb5e3270864fea69fa989f40d64a9dd9ac92f844 Mon Sep 17 00:00:00 2001
From: Mike Williams <mike@mikebwilliams.com>
Date: Wed, 15 Nov 2023 10:43:33 -0500
Subject: [PATCH] kicad-cli: support * wildcard in BOM export

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/16086
---
 eeschema/eeschema_jobs_handler.cpp | 40 ++++++++++++++++++++++++++++++
 kicad/cli/command_sch_export_bom.h |  2 +-
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/eeschema/eeschema_jobs_handler.cpp b/eeschema/eeschema_jobs_handler.cpp
index c89d9a1b91..601509b5e3 100644
--- a/eeschema/eeschema_jobs_handler.cpp
+++ b/eeschema/eeschema_jobs_handler.cpp
@@ -449,6 +449,46 @@ int EESCHEMA_JOBS_HANDLER::JobExportBom( JOB* aJob )
 
         for( wxString fieldName : aBomJob->m_fieldsOrdered )
         {
+            // Handle wildcard. We allow the wildcard anywhere in the list, but it needs to respect
+            // fields that come before and after the wildcard.
+            if( fieldName == _( "*" ) )
+            {
+                for( const BOM_FIELD& modelField : dataModel.GetFieldsOrdered() )
+                {
+                    struct BOM_FIELD field;
+
+                    field.name = modelField.name;
+                    field.show = true;
+                    field.groupBy = false;
+                    field.label = field.name;
+
+                    bool fieldAlreadyPresent = false;
+                    for( BOM_FIELD& presetField : preset.fieldsOrdered )
+                    {
+                        if( presetField.name == field.name )
+                        {
+                            fieldAlreadyPresent = true;
+                            break;
+                        }
+                    }
+
+                    bool fieldLaterInList = false;
+                    for( const wxString& fieldInList : aBomJob->m_fieldsOrdered )
+                    {
+                        if( fieldInList == field.name )
+                        {
+                            fieldLaterInList = true;
+                            break;
+                        }
+                    }
+
+                    if( !fieldAlreadyPresent && !fieldLaterInList )
+                        preset.fieldsOrdered.emplace_back( field );
+                }
+
+                continue;
+            }
+
             struct BOM_FIELD field;
 
             field.name = fieldName;
diff --git a/kicad/cli/command_sch_export_bom.h b/kicad/cli/command_sch_export_bom.h
index 4a6871a274..63f9d2baf4 100644
--- a/kicad/cli/command_sch_export_bom.h
+++ b/kicad/cli/command_sch_export_bom.h
@@ -53,7 +53,7 @@ namespace CLI
 
 //Options for controlling the fields and the grouping
 #define ARG_FIELDS "--fields"
-#define ARG_FIELDS_DESC "An ordered list of fields to export."
+#define ARG_FIELDS_DESC "An ordered list of fields to export. Supports * and ${} substitutions."
 
 #define ARG_LABELS "--labels"
 #define ARG_LABELS_DESC "An ordered list of labels to apply the exported fields."