Method: Yast::CommandLineClass#Init

Defined in:
library/commandline/src/modules/CommandLine.rb

#Init(cmdlineinfo, args) ⇒ Object

Initialize Module

Initialize the module, setup the command line syntax and arguments passed on the command line.

@param [Hash] cmdlineinfo the map describing the module command line @param [Array] args arguments given by the user on the command line @return [Boolean] true, if there are some commands to be processed (and cmdlineinfo passes sanity checks) @see #Command



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'library/commandline/src/modules/CommandLine.rb', line 1052

def Init(cmdlineinfo, args)
  cmdlineinfo = deep_copy(cmdlineinfo)
  args = deep_copy(args)
  # remember the command line specification
  # required later by xmlhelp command
  @cmdlinespec = deep_copy(cmdlineinfo)

  cmdline_supported = true

  # check whether the command line mode is really supported by the module
  if !Builtins.haskey(cmdlineinfo, "actions") ||
      Builtins.size(Ops.get_map(cmdlineinfo, "actions", {})) == 0
    cmdline_supported = false
  end

  # initialize verbose flag
  @verbose = Builtins.contains(WFM.Args, "verbose")

  id_string = Ops.get_string(cmdlineinfo, "id", "")
  # sanity checks on cmdlineinfo
  # check for id string , it must exist, and non-empty
  if cmdline_supported && (id_string == "" || !Ops.is_string?(id_string))
    Builtins.y2error("Command line specification does not define module id")

    # use 'unknown' as id
    cmdlineinfo = Builtins.remove(cmdlineinfo, "id") if Builtins.haskey(cmdlineinfo, "id")

    # translators: fallback name for a module at command line
    cmdlineinfo = Builtins.add(cmdlineinfo, "id", _("unknown"))

    # it's better to abort now
    @done = true
    @aborted = true
  end

  # check for helps, they are required everywhere
  # global help text
  if cmdline_supported && !Builtins.haskey(cmdlineinfo, "help")
    Builtins.y2error(
      "Command line specification does not define global help for the module"
    )

    # it's better to abort now
    @done = true
    @aborted = true
  end

  # help texts for actions
  if Builtins.haskey(cmdlineinfo, "actions")
    Builtins.foreach(Ops.get_map(cmdlineinfo, "actions", {})) do |action, def_|
      if !Builtins.haskey(def_, "help")
        Builtins.y2error(
          "Command line specification does not define help for action '%1'",
          action
        )

        # it's better to abort now
        @done = true
        @aborted = true
      end
    end
  end

  # help for options
  if Builtins.haskey(cmdlineinfo, "options")
    Builtins.foreach(Ops.get_map(cmdlineinfo, "options", {})) do |option, def_|
      if !Builtins.haskey(def_, "help")
        Builtins.y2error(
          "Command line specification does not define help for option '%1'",
          option
        )

        # it's better to abort now
        @done = true
        @aborted = true
      end
      # check that regex and enum have defined typespec
      if (Ops.get_string(def_, "type", "") == "regex" ||
          Ops.get_string(def_, "type", "") == "enum") &&
          !Builtins.haskey(def_, "typespec")
        Builtins.y2error(
          "Command line specification does not define typespec for option '%1'",
          option
        )

        # it's better to abort now
        @done = true
        @aborted = true
      end
    end
  end

  # mappings - check for existing actions and options
  if Builtins.haskey(cmdlineinfo, "mappings")
    Builtins.foreach(Ops.get_map(cmdlineinfo, "mappings", {})) do |mapaction, def_|
      # is this action defined?
      if !Builtins.haskey(
        Ops.get_map(cmdlineinfo, "actions", {}),
        mapaction
      )
        Builtins.y2error(
          "Command line specification maps undefined action '%1'",
          mapaction
        )

        # it's better to abort now
        @done = true
        @aborted = true
      end
      Builtins.foreach(def_) do |mapopt|
        next if !Ops.is_string?(mapopt)

        # is this option defined?
        if !Builtins.haskey(
          Ops.get_map(cmdlineinfo, "options", {}),
          Convert.to_string(mapopt)
        )
          Builtins.y2error(
            "Command line specification maps undefined option '%1' for action '%2'",
            mapopt,
            mapaction
          )

          # it's better to abort now
          @done = true
          @aborted = true
        end
      end
    end
  end

  return false if @done

  @modulecommands = deep_copy(cmdlineinfo)

  # build allcommands - help and verbose options are added specially
  @allcommands = {
    "actions"  => Builtins.union(
      Ops.get_map(@modulecommands, "actions", {}),
      Ops.get(@systemcommands, "actions", {})
    ),
    "options"  => Builtins.union(
      Ops.get_map(@modulecommands, "options", {}),
      Ops.get(@systemcommands, "options", {})
    ),
    "mappings" => Builtins.union(
      Builtins.mapmap(Ops.get_map(@modulecommands, "mappings", {})) do |act, opts|
        { act => Builtins.union(opts, ["help", "verbose"]) }
      end,
      Ops.get(@systemcommands, "mappings", {})
    )
  }

  if Ops.less_than(Builtins.size(args), 1) || Stage.stage != "normal" ||
      Stage.firstboot
    Mode.SetUI("dialog")
    # start GUI, module has some work to do :-)
    return true
  else
    Mode.SetUI("commandline")
  end

  if !cmdline_supported
    # command line is not supported
    Print(
      String.UnderlinedHeader(
        Ops.add("YaST2 ", Ops.get_string(cmdlineinfo, "id", "")),
        0
      )
    )
    Print("")

    help = Ops.get_string(cmdlineinfo, "help", "")
    if !help.nil? && help != ""
      Print(Ops.get_string(cmdlineinfo, "help", ""))
      Print("")
    end

    Print(@nosupport)
    Print("")
    return false
  end

  # setup prompt
  @cmdlineprompt = Ops.add(
    Ops.add("YaST2 ", Ops.get_string(cmdlineinfo, "id", "")),
    "> "
  )
  SCR.Write(path(".dev.tty.prompt"), @cmdlineprompt)

  # parse args
  @commandcache = Parse(args)

  # return true, if there is some work to do:
  # first, try to interpret system commands
  if ProcessSystemCommands(@commandcache)
    # it was system command, there is work only in interactive mode
    @commandcache = {}
    @done = !@interactive
    @aborted = false
    @interactive
  else
    # we cannot handle this on our own, return true if there is some command to be processed
    # i.e, there is no parsing error
    @done = Builtins.size(@commandcache) == 0
    @aborted = @done
    !@done
  end
end