@@ -791,6 +791,15 @@ final protected function applyOptionValues(
791791 $ _values [$ name ] = $ _value ;
792792 $ this ->OptionValues [$ option ->Key ] = $ _value ;
793793 if ($ asArguments ) {
794+ // If the option has an optional value and no value was given,
795+ // store null to ensure it's not expanded on export
796+ if (
797+ $ option ->ValueOptional &&
798+ $ option ->ValueType !== CliOptionValueType::BOOLEAN &&
799+ $ value === true
800+ ) {
801+ $ value = null ;
802+ }
794803 $ this ->ArgumentValues [$ option ->Key ] = $ value ;
795804 }
796805 }
@@ -834,21 +843,29 @@ final protected function normaliseOptionValues(
834843 * returned.
835844 * @param bool $schema If `true`, an array that maps schema option names to
836845 * values is returned.
846+ * @param bool $unexpand If `true` and an option has an optional value not
847+ * given on the command line, replace its value with `null` or `true`.
837848 * @return array<string,mixed>
838849 */
839850 final protected function getOptionValues (
840851 bool $ export = false ,
841- bool $ schema = false
852+ bool $ schema = false ,
853+ bool $ unexpand = false
842854 ): array {
843855 $ this ->assertHasRun ()->loadOptions ();
844856 $ options = $ schema ? $ this ->SchemaOptions : $ this ->Options ;
845857 foreach ($ options as $ key => $ option ) {
846- if ($ export && !array_key_exists ($ option ->Key , $ this ->ArgumentValues )) {
858+ $ given = array_key_exists ($ option ->Key , $ this ->ArgumentValues );
859+ if ($ export && !$ given ) {
860+ continue ;
861+ }
862+ if ($ option ->ValueOptional && !$ option ->Required && !$ given ) {
847863 continue ;
848864 }
849865 $ name = $ schema ? $ key : $ option ->Name ;
850866 if (
851- $ export &&
867+ $ unexpand &&
868+ $ given &&
852869 $ option ->ValueOptional &&
853870 $ this ->ArgumentValues [$ option ->Key ] === null
854871 ) {
0 commit comments