1
1
/*
2
- * Copyright (c) 2020, 2022 , Oracle and/or its affiliates.
2
+ * Copyright (c) 2020, 2023 , Oracle and/or its affiliates.
3
3
*
4
4
* This program is free software; you can redistribute it and/or modify
5
5
* it under the terms of the GNU General Public License, version 2.0,
@@ -553,11 +553,15 @@ Provider *Shell_cli_mapper::identify_operation(Provider *base_provider) {
553
553
}
554
554
555
555
void Shell_cli_mapper::add_argument (const shcore::Value &argument) {
556
- for (size_t index = 0 ; index < m_missing_optionals; index++) {
557
- m_argument_list->push_back (shcore::Value::Null ());
558
- }
556
+ add_default_arguments ();
559
557
m_argument_list->push_back (argument);
560
- m_missing_optionals = 0 ;
558
+ m_missing_optionals.clear ();
559
+ }
560
+
561
+ void Shell_cli_mapper::add_default_arguments () {
562
+ for (auto &missing_optional : m_missing_optionals) {
563
+ m_argument_list->push_back (std::move (missing_optional));
564
+ }
561
565
}
562
566
563
567
/* *
@@ -675,13 +679,21 @@ void Shell_cli_mapper::process_positional_args() {
675
679
m_cmdline_args.erase (m_cmdline_args.begin ());
676
680
} else {
677
681
if (connection_data->empty ()) {
678
- // If the parameter is empty, inserts a null if not optional,
679
- // otherwise counts it as parameters to be added in case a further
680
- // parameter has value
682
+ // If a connection parameter is empty CLI determines the value to
683
+ // use as follows:
684
+ // - If required: Uses Null
685
+ // - If optional, Caches it's default value if any, or Null
681
686
if (m_metadata->signature [index]->flag ==
682
- shcore::Param_flag::Optional)
683
- m_missing_optionals++;
684
- else
687
+ shcore::Param_flag::Optional) {
688
+ if (m_metadata->signature [index]->def_value .type !=
689
+ shcore::Value_type::Undefined) {
690
+ m_missing_optionals.push_back (
691
+ m_metadata->signature [index]->def_value );
692
+
693
+ } else {
694
+ m_missing_optionals.push_back (shcore::Value::Null ());
695
+ }
696
+ } else
685
697
add_argument (shcore::Value::Null ());
686
698
} else {
687
699
add_argument (shcore::Value (connection_data));
@@ -691,15 +703,19 @@ void Shell_cli_mapper::process_positional_args() {
691
703
case ' D' : {
692
704
const auto &options = get_argument_options (param_name).as_map ();
693
705
694
- if (options->empty ()) {
695
- // If the parameter is empty, inserts a null if not optional,
696
- // otherwise counts it as parameters to be added in case a further
697
- // parameter has value
698
- if (m_metadata->signature [index]->flag ==
699
- shcore::Param_flag::Optional)
700
- m_missing_optionals++;
701
- else
702
- add_argument (shcore::Value::Null ());
706
+ if (options->empty () && m_metadata->signature [index]->flag ==
707
+ shcore::Param_flag::Optional) {
708
+ // If a dictionary parameter is empty CLI determines the value to
709
+ // use as follows:
710
+ // - If required: Uses an empty dictionary
711
+ // - If optional, Caches it's default value if any, or an empty dict
712
+ if (m_metadata->signature [index]->def_value .type !=
713
+ shcore::Value_type::Undefined) {
714
+ m_missing_optionals.push_back (
715
+ m_metadata->signature [index]->def_value );
716
+ } else {
717
+ m_missing_optionals.push_back (shcore::Value (options));
718
+ }
703
719
} else {
704
720
add_argument (shcore::Value (options));
705
721
}
@@ -709,35 +725,46 @@ void Shell_cli_mapper::process_positional_args() {
709
725
shcore::Array_t list_argument;
710
726
if (has_argument_options (param_name)) {
711
727
list_argument = get_argument_options (param_name).as_array ();
712
- } else if (!m_cmdline_args. empty ()) {
728
+ } else {
713
729
list_argument = shcore::make_array ();
714
- auto validator =
715
- m_metadata->signature [index]->validator <List_validator>();
716
-
717
- // All remaining anonymous arguments are added to the list parameter
718
- while (!m_cmdline_args.empty ()) {
719
- // Only anonymous arguments are added to the list
720
- if (m_cmdline_args[0 ].option .empty ()) {
721
- if (m_cmdline_args[0 ].value .type == shcore::Array) {
722
- validate_list_items (m_cmdline_args[0 ],
730
+ if (!m_cmdline_args.empty ()) {
731
+ auto validator =
732
+ m_metadata->signature [index]->validator <List_validator>();
733
+
734
+ // All remaining anonymous arguments are added to the list parameter
735
+ while (!m_cmdline_args.empty ()) {
736
+ // Only anonymous arguments are added to the list
737
+ if (m_cmdline_args[0 ].option .empty ()) {
738
+ if (m_cmdline_args[0 ].value .type == shcore::Array) {
739
+ validate_list_items (m_cmdline_args[0 ],
740
+ validator->element_type ());
741
+ auto source = m_cmdline_args[0 ].value .as_array ();
742
+ std::move (source->begin (), source->end (),
743
+ std::back_inserter (*list_argument));
744
+ } else {
745
+ add_value_to_list (&list_argument, m_cmdline_args[0 ],
723
746
validator->element_type ());
724
- auto source = m_cmdline_args[0 ].value .as_array ();
725
- std::move (source->begin (), source->end (),
726
- std::back_inserter (*list_argument));
727
- } else {
728
- add_value_to_list (&list_argument, m_cmdline_args[0 ],
729
- validator->element_type ());
747
+ }
730
748
}
731
- }
732
749
733
- m_cmdline_args.erase (m_cmdline_args.begin ());
750
+ m_cmdline_args.erase (m_cmdline_args.begin ());
751
+ }
734
752
}
735
753
}
736
754
737
- if ((!list_argument || list_argument->empty ()) &&
738
- m_metadata->signature [index]->flag ==
739
- shcore::Param_flag::Optional) {
740
- m_missing_optionals++;
755
+ if (list_argument->empty () && m_metadata->signature [index]->flag ==
756
+ shcore::Param_flag::Optional) {
757
+ // If a list parameter is empty CLI determines the value to use as
758
+ // follows:
759
+ // - If required: Uses an empty list
760
+ // - If optional, Caches it's default value if any, or an empty list
761
+ if (m_metadata->signature [index]->def_value .type !=
762
+ shcore::Value_type::Undefined) {
763
+ m_missing_optionals.push_back (
764
+ m_metadata->signature [index]->def_value );
765
+ } else {
766
+ m_missing_optionals.push_back (shcore::Value (list_argument));
767
+ }
741
768
} else {
742
769
add_argument (shcore::Value (list_argument));
743
770
}
@@ -753,10 +780,24 @@ void Shell_cli_mapper::process_positional_args() {
753
780
m_cmdline_args.erase (m_cmdline_args.begin ());
754
781
}
755
782
783
+ // If any other parameter is undefined CLI determines the value to use
784
+ // as follows:
785
+ // - If required: Uses Null
786
+ // - If optional, Caches it's default value if any, or Null
787
+ // list
788
+
756
789
if (value.type == shcore::Undefined) {
757
790
if (m_metadata->signature [index]->flag ==
758
791
shcore::Param_flag::Optional) {
759
- m_missing_optionals++;
792
+ if (m_metadata->signature [index]->def_value .type !=
793
+ shcore::Value_type::Undefined) {
794
+ m_missing_optionals.push_back (
795
+ m_metadata->signature [index]->def_value );
796
+ } else {
797
+ m_missing_optionals.push_back (shcore::Value::Null ());
798
+ }
799
+ } else {
800
+ add_argument (shcore::Value::Null ());
760
801
}
761
802
} else {
762
803
add_argument (value);
@@ -809,7 +850,7 @@ void Shell_cli_mapper::clear() {
809
850
m_argument_options.clear ();
810
851
m_cli_option_registry.clear ();
811
852
m_normalized_cli_options.clear ();
812
- m_missing_optionals = 0 ;
853
+ m_missing_optionals. clear () ;
813
854
m_waiting_value = false ;
814
855
m_help_type = Help_type::NONE;
815
856
m_cmdline_args.clear ();
@@ -927,13 +968,16 @@ void Shell_cli_mapper::define_named_args() {
927
968
m_argument_options[param_name] = shcore::Value (shcore::make_dict ());
928
969
} break ;
929
970
case ' A' :
971
+ // If a single array parameter is defined, its items can not be handled
972
+ // as named args, however, any further list are handled as named
973
+ // parameters
930
974
if (!found_list) {
931
975
found_list = true ;
932
976
} else {
933
977
define_named_arg (param_name, " " , " " ,
934
978
m_metadata->signature [index]->flag ==
935
979
shcore::Param_flag::Mandatory);
936
- m_argument_options[param_name] = shcore::Value ( shcore::Array_t () );
980
+ m_argument_options[param_name] = shcore::Value::new_array ( );
937
981
}
938
982
break ;
939
983
default :
@@ -1008,6 +1052,8 @@ void Shell_cli_mapper::process_arguments(shcore::Argument_list *argument_list) {
1008
1052
1009
1053
process_named_args ();
1010
1054
process_positional_args ();
1055
+ // Adds any remaining argument with a default value
1056
+ add_default_arguments ();
1011
1057
}
1012
1058
1013
1059
} // namespace cli
0 commit comments