Class: Yast::CommandLineClass

Inherits:
Module
  • Object
show all
Includes:
Logger
Defined in:
library/commandline/src/modules/CommandLine.rb

Instance Method Summary collapse

Instance Method Details

#AbortObject

Abort the command line handling



1384
1385
1386
1387
1388
1389
# File 'library/commandline/src/modules/CommandLine.rb', line 1384

def Abort
  @aborted = true
  @done = true

  nil
end

#AbortedObject

User asked for abort (forgetting the changes)

@return [Boolean] true, if the user asked abort



1379
1380
1381
# File 'library/commandline/src/modules/CommandLine.rb', line 1379

def Aborted
  @aborted
end

#CommandObject

Get next user-given command

Get next user-given command. If there is a command available, returns it, otherwise ask the user for a command (in interactive mode). Also processes system commands.

@return [Hash] of the new command. If there are no more commands, it returns exit or abort depending on the result user asked for.

@see #Parse



1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
# File 'library/commandline/src/modules/CommandLine.rb', line 1321

def Command
  # if we are done already, return the result
  return { "command" => @aborted ? "abort" : "exit" } if @done

  # there is a command in the cache
  if Builtins.size(@commandcache) != 0
    result = deep_copy(@commandcache)
    @commandcache = {}
    @done = !@interactive
    deep_copy(result)
  # if in interactive mode, ask user for input
  elsif @interactive
    loop do
      newcommand = []
      newcommand = Scan() while Builtins.size(newcommand) == 0

      # EOF reached
      if newcommand.nil?
        @done = true
        return { "command" => "exit" }
      end

      @commandcache = Parse(newcommand)
      break if !ProcessSystemCommands(@commandcache)
      break if @done
    end

    return { "command" => @aborted ? "abort" : "exit" } if @done

    # we are not done, return the command asked back to module
    result = deep_copy(@commandcache)
    @commandcache = {}

    deep_copy(result)
  else
    # there is no further commands left
    @done = true
    { "command" => "exit" }
  end
end

#DoneObject

Are there some commands to be processed?

@return [Boolean] true, if there is no more commands to be processed, either because the user used command line, or the interactive mode was finished



1395
1396
1397
# File 'library/commandline/src/modules/CommandLine.rb', line 1395

def Done
  @done
end

#Error(message) ⇒ Object

Print an Error Message

Print an error message and add the description how to get the help.

Parameters:

  • message (String)

    error message to be printed. Use nil for no message



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'library/commandline/src/modules/CommandLine.rb', line 324

def Error(message)
  Print(message) if !message.nil?

  if @interactive
    # translators: default error message for command line
    Print(_("Use 'help' for a complete list of available commands."))
  else
    # translators: default error message for command line
    Print(
      Builtins.sformat(
        _("Use 'yast2 %1 help' for a complete list of available commands."),
        Ops.get_string(@modulecommands, "id", "")
      )
    )
  end

  nil
end

#GetInput(prompt, type) ⇒ String

Set prompt and read input from command line

Parameters:

  • prompt (String)

    Set prompt

  • type (Symbol)

    Type

Returns:



1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
# File 'library/commandline/src/modules/CommandLine.rb', line 1276

def GetInput(prompt, type)
  # set the required prompt
  SCR.Write(path(".dev.tty.prompt"), prompt)

  res = case type
  when :nohistory
    Convert.to_string(SCR.Read(path(".dev.tty.nohistory")))
  when :noecho
    Convert.to_string(SCR.Read(path(".dev.tty.noecho")))
  else
    Convert.to_string(SCR.Read(path(".dev.tty")))
  end

  # set the default prompt
  SCR.Write(path(".dev.tty.prompt"), @cmdlineprompt)

  res
end

#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

#InteractiveObject

Is module started in interactive command-line mode?

@return [Boolean] true, if the user asked for interactive command-line mode



1372
1373
1374
# File 'library/commandline/src/modules/CommandLine.rb', line 1372

def Interactive
  @interactive
end

#mainObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'library/commandline/src/modules/CommandLine.rb', line 34

def main
  Yast.import "Directory"
  Yast.import "Mode"
  Yast.import "Popup"
  Yast.import "Report"
  Yast.import "Stage"
  Yast.import "String"
  Yast.import "Integer"
  Yast.import "TypeRepository"
  Yast.import "XML"

  textdomain "base"

  @cmdlineprompt = "YaST2 > "

  # Map of commands for every module. ATM the list of commands this module handles internally.
  @systemcommands = {
    "actions"  => {
      "help"        => {
        # translators: help for 'help' option on command line
        "help"     => _(
          "Print the help for this module"
        ),
        "readonly" => true
      },
      "longhelp"    => {
        # translators: help for 'longhelp' option on command line
        "help"     => _(
          "Print a long version of help for this module"
        ),
        "readonly" => true
      },
      "xmlhelp"     => {
        # translators: help for 'xmlhelp' option on command line
        "help"     => _(
          "Print a long version of help for this module in XML format"
        ),
        "readonly" => true
      },
      "interactive" => {
        # translators: help for 'interactive' option on command line
        "help"     => _(
          "Start interactive shell to control the module"
        ),
        # interactive mode itself does not mean that write is needed.
        # The first action that is not readonly will switch flag.
        "readonly" => true
      },
      "exit"        => {
        # translators: help for 'exit' command line interactive mode
        "help"     => _(
          "Exit interactive mode and save the changes"
        ),
        "readonly" => true
      },
      "abort"       => {
        # translators: help for 'abort' command line interactive mode
        "help"     => _(
          "Abort interactive mode without saving the changes"
        ),
        "readonly" => true
      }
    },
    "options"  => {
      "help"    => {
        # translators:  command line "help" option
        "help" => _(
          "Print the help for this command"
        )
      },
      "verbose" => {
        # translators: command line "verbose" option
        "help" => _(
          "Show progress information"
        )
      },
      "xmlfile" => {
        # translators: command line "xmlfile" option
        "help" => _(
          "Where to store the XML output"
        ),
        "type" => "string"
      }
    },
    "mappings" => {
      "help"        => ["help", "verbose"],
      "xmlhelp"     => ["help", "verbose", "xmlfile"],
      "interactive" => ["help", "verbose"],
      "exit"        => ["help"],
      "abort"       => ["help"]
    }
  }

  # Map of commands defined by the YaST2 module.
  @modulecommands = {}

  # Merged map of commands - both defined by the YaST2 module and system commands. Used for lookup
  @allcommands = deep_copy(@systemcommands)

  # User asked for interactive session
  @interactive = false

  # All commands have been processed
  @done = false

  # User asked for quitting of interactive session, or there was an error
  @aborted = false

  # a cache for already parsed but not processed command
  @commandcache = {}

  # Verbose mode flag
  @verbose = false

  # Remember the command line specification for later use
  @cmdlinespec = {}

  # string: command line interface is not supported
  @nosupport = _(
    "This YaST2 module does not support the command line interface."
  )
end

#Parse(arguments) ⇒ Object

Parse a list of arguments.

It checks the validity of the arguments, the type correctness and returns the command and its options in a map. @param [Array] arguments the list of arguments to be parsed @return [Hash=> Object] containing the command and it's option. In case of error it is an empty map.



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'library/commandline/src/modules/CommandLine.rb', line 350

def Parse(arguments)
  args = deep_copy(arguments)
  return {} if Ops.less_than(Builtins.size(args), 1)

  # Parse command
  command = args.shift
  Builtins.y2debug("command=%1", command)
  Builtins.y2debug("args=%1", args)

  if command == ""
    Builtins.y2error(
      "CommandLine::Parse called with first parameter being empty. Arguments passed: %1",
      arguments
    )
    return {}
  end

  # Check command
  if !Builtins.haskey(Ops.get_map(@allcommands, "actions", {}), command)
    # translators: error message in command line interface
    Error(Builtins.sformat(_("Unknown Command: %1"), command))

    return {}
  end

  # build the list of options for the command
  opts = Ops.get_list(@allcommands, ["mappings", command], [])
  allopts = Ops.get_map(@allcommands, "options", {})
  cmdoptions = {}
  Builtins.maplist(opts) do |k|
    cmdoptions = Builtins.add(cmdoptions, k, Ops.get_map(allopts, k, {})) if Ops.is_string?(k)
  end

  ret = true

  # Parse options
  givenoptions = {}
  Builtins.maplist(args) do |aos|
    Builtins.y2debug("os=%1", aos)
    next if !Ops.is_string?(aos)

    os = Convert.to_string(aos)
    o = Builtins.regexptokenize(os, "([^=]+)=(.+)")
    Builtins.y2debug("o=%1", o)
    case Builtins.size(o)
    when 2
      givenoptions = Builtins.add(
        givenoptions,
        Ops.get(o, 0, ""),
        Ops.get(o, 1, "")
      )
    when 0
      # check, if the last character is "="
      # FIXME: consider whitespace
      if Builtins.substring(os, Ops.subtract(Builtins.size(os), 1)) == "="
        # translators: error message - user did not provide a value for option %1 on the command line
        Print(
          Builtins.sformat(
            _("Option '%1' is missing value."),
            Builtins.substring(os, 0, Ops.subtract(Builtins.size(os), 1))
          )
        )
        @aborted = true if !@interactive
        ret = false
        next {}
      else
        givenoptions = Builtins.add(givenoptions, os, "")
      end
    end
  end

  return {} if ret != true

  Builtins.y2debug("options=%1", givenoptions)

  # Check options

  # find out, if the action has a "non-strict" option set
  non_strict = Builtins.contains(
    Ops.get_list(@allcommands, ["actions", command, "options"], []),
    "non_strict"
  )
  Builtins.y2debug("Using non-strict check for %1", command) if non_strict

  # check (and convert data types)
  Builtins.maplist(givenoptions) do |o, val|
    v = Convert.to_string(val)
    next if ret != true

    if Ops.get(cmdoptions, o).nil?
      if !non_strict
        # translators: error message, %1 is a command, %2 is the wrong option given by the user
        Print(
          Builtins.sformat(
            _("Unknown option for command '%1': %2"),
            command,
            o
          )
        )
        @aborted = true if !@interactive
        ret = false
      end
    else
      # this option is valid, let's check the type

      opttype = Ops.get_string(cmdoptions, [o, "type"], "")

      if opttype != ""
        # need to check the type
        case opttype
        when "regex"
          opttypespec = Ops.get_string(cmdoptions, [o, "typespec"], "")
          ret = TypeRepository.regex_validator(opttypespec, v)
          if ret != true
            # translators: error message, %2 is the value given
            Print(
              Builtins.sformat(_("Invalid value for option '%1': %2"), o, v)
            )
            @aborted = true if !@interactive
          end
        when "enum"
          ret = TypeRepository.enum_validator(
            Ops.get_list(cmdoptions, [o, "typespec"], []),
            v
          )
          if ret != true
            # translators: error message, %2 is the value given
            Print(
              Builtins.sformat(_("Invalid value for option '%1': %2"), o, v)
            )
            @aborted = true if !@interactive
          end
        when "integer"
          i = Builtins.tointeger(v)
          ret = !i.nil?
          if ret == true
            # update value of the option to integer
            Ops.set(givenoptions, o, i)
          else
            # translators: error message, %2 is the value given
            Print(
              Builtins.sformat(_("Invalid value for option '%1': %2"), o, v)
            )
            @aborted = true if !@interactive
          end
        else
          ret = (v == "") ? false : TypeRepository.is_a(v, opttype)

          if ret != true
            # translators: error message, %2 is expected type, %3 is the value given
            Print(
              Builtins.sformat(
                _(
                  "Invalid value for option '%1' -- expected '%2', received %3"
                ),
                o,
                opttype,
                v
              )
            )
            @aborted = true if !@interactive
          end
        end
      # type is missing
      elsif v != ""
        Builtins.y2error(
          "Type specification for option '%1' is missing, cannot assign a value to the option",
          o
        )
        # translators: error message if option has a value, but cannot have one
        Print(
          Builtins.sformat(
            _("Option '%1' cannot have a value. Given value: %2"),
            o,
            v
          )
        )
        @aborted = true if !@interactive
        ret = false
      end
    end
  end

  # wrong, let's print the help message
  if ret != true
    if @interactive
      # translators: error message, how to get command line help for interactive mode
      # %1 is the module name, %2 is the action name
      Print(
        Builtins.sformat(
          _("Use '%1 %2 help' for a complete list of available options."),
          Ops.get_string(@modulecommands, "id", ""),
          command
        )
      )
    else
      # translators: error message, how to get command line help for non-interactive mode
      # %1 is the module name, %2 is the action name
      Print(
        Builtins.sformat(
          _(
            "Use 'yast2 %1 %2 help' for a complete list of available options."
          ),
          Ops.get_string(@modulecommands, "id", ""),
          command
        )
      )
    end
    return {}
  end

  { "command" => command, "options" => givenoptions }
end

#PasswordInput(prompt) ⇒ String

Read input from command line

Read input from command line, input is not displayed and not stored in the command line history. This function should be used for reading a password.

Parameters:

  • prompt (String)

    Set prompt to this value

Returns:



1308
1309
1310
# File 'library/commandline/src/modules/CommandLine.rb', line 1308

def PasswordInput(prompt)
  GetInput(prompt, :noecho)
end

Print a String

Print a string to /dev/tty in interactive mode, to stderr in non-interactive Suppress printing if there are no commands to be handled (starting GUI)

@param [String] string to be printed



194
195
196
# File 'library/commandline/src/modules/CommandLine.rb', line 194

def Print(string)
  PrintInternal(string, true)
end

#PrintActionHelp(action) ⇒ Object

Print a help text for a given action.

Parameters:

  • action (String)

    the action for which the help should be printed



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'library/commandline/src/modules/CommandLine.rb', line 580

def PrintActionHelp(action)
  # lookup action in actions
  command = Ops.get_map(@allcommands, ["actions", action], {})
  # translators: the command does not provide any help
  commandhelp = Ops.get(command, "help")
  commandhelp = _("No help available") if commandhelp.nil?
  has_string_option = false
  # Process <command> "help"
  # translators: %1 is the command name
  Print(Builtins.sformat(_("Command '%1'"), action))

  # print help
  if Ops.is_string?(commandhelp)
    Print(Builtins.sformat("    %1", commandhelp))
  elsif Ops.is(commandhelp, "list <string>")
    Builtins.foreach(
      Convert.convert(commandhelp, from: "any", to: "list <string>")
    ) { |e| Print(Builtins.sformat("    %1", e)) }
  end

  opts = Ops.get_list(@allcommands, ["mappings", action], [])

  # no options, skip the rest
  if Builtins.size(opts) == 0
    Print("")
    return
  end

  # translators: command line options
  Print(_("\n    Options:"))

  allopts = Ops.get_map(@allcommands, "options", {})

  longestopt = 0
  longestarg = 0

  Builtins.foreach(opts) do |opt|
    op = Ops.get_map(allopts, opt, {})
    t = Ops.get_string(op, "type", "")
    has_string_option = true if t == "string"
    if t != "regex" && t != "enum" && t != ""
      t = Ops.add(Ops.add("[", t), "]")
    elsif t == "enum"
      t = "[ "
      Builtins.foreach(Ops.get_list(op, "typespec", [])) do |s|
        t = Ops.add(Ops.add(t, s), " ")
      end
      t = Ops.add(t, "]")
    end
    longestarg = Builtins.size(t) if Ops.greater_than(Builtins.size(t), longestarg)
    if Ops.is_string?(opt) &&
        Ops.greater_than(Builtins.size(Convert.to_string(opt)), longestopt)
      longestopt = Builtins.size(Convert.to_string(opt))
    end
  end

  Builtins.foreach(opts) do |opt|
    op = Ops.get_map(allopts, opt, {})
    t = Ops.get_string(op, "type", "")
    if t != "regex" && t != "enum" && t != ""
      t = Ops.add(Ops.add("[", t), "]")
    elsif t == "enum"
      t = "[ "
      Builtins.foreach(Ops.get_list(op, "typespec", [])) do |s|
        t = Ops.add(Ops.add(t, s), " ")
      end
      t = Ops.add(t, "]")
    else
      t = "    "
    end
    if Ops.is_string?(opt)
      helptext = ""
      opthelp = Ops.get(op, "help")

      if Ops.is_string?(opthelp)
        helptext = Convert.to_string(opthelp)
      elsif Ops.is(opthelp, "map <string, string>")
        helptext = Ops.get(
          Convert.convert(
            opthelp,
            from: "any",
            to:   "map <string, string>"
          ),
          action,
          ""
        )
      elsif Ops.is(opthelp, "list <string>")
        delim = Builtins.sformat(
          "\n        %1  %2  ",
          String.Pad("", longestopt),
          String.Pad("", longestarg)
        )
        helptext = Builtins.mergestring(
          Convert.convert(opthelp, from: "any", to: "list <string>"),
          delim
        )
      else
        Builtins.y2error(
          "Invalid data type of help text, only 'string' or 'map<string,string>' types are allowed."
        )
      end

      Print(
        Builtins.sformat(
          "        %1  %2  %3",
          String.Pad(Convert.to_string(opt), longestopt),
          String.Pad(t, longestarg),
          helptext
        )
      )
    end
  end

  if has_string_option
    # additional help for using command line
    Print(
      _(
        "\n    Options of the [string] type must be written in the form 'option=value'."
      )
    )
  end
  if Builtins.haskey(command, "example")
    # translators: example title for command line
    Print(_("\n    Example:"))

    example = Ops.get(command, "example")

    if Ops.is_string?(example)
      Print(Builtins.sformat("        %1", example))
    elsif Ops.is(example, "list <string>")
      Builtins.foreach(
        Convert.convert(example, from: "any", to: "list <string>")
      ) { |e| Print(Builtins.sformat("        %1", e)) }
    else
      Builtins.y2error("Unsupported data type - value: %1", example)
    end
  end
  Print("")

  nil
end

#PrintGeneralHelpObject

Print a general help - list of available command.



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'library/commandline/src/modules/CommandLine.rb', line 723

def PrintGeneralHelp
  # display custom defined help instead of generic one
  if Builtins.haskey(@modulecommands, "customhelp")
    Print(Ops.get_string(@modulecommands, "customhelp", ""))
    return
  end

  # translators: default module description if none is provided by the module itself
  Print(
    Ops.add(
      Ops.get_locale(@modulecommands, "help", _("This is a YaST module.")),
      "\n"
    )
  )
  # translators: short help title for command line
  Print(_("Basic Syntax:"))

  if @interactive
    # translators: module command line help
    # translate <command> and [options] only!
    Print(_("    <command> [options]"))
    # translators: module command line help
    # translate <command> only!
    Print(_("    <command> help"))
    # translators: module command line help
    Print("    help")
    Print("    longhelp")
    Print("    xmlhelp")
    Print("")
    Print("    exit")
    Print("    abort")
  else
    # translators: module command line help, %1 is the module name
    Print(
      Builtins.sformat(
        "    yast2 %1 interactive",
        Ops.get_string(@modulecommands, "id", "")
      )
    )

    # translators: module command line help, %1 is the module name
    # translate <command> and [options] only!
    Print(
      Builtins.sformat(
        _("    yast2 %1 <command> [verbose] [options]"),
        Ops.get_string(@modulecommands, "id", "")
      )
    )
    # translators: module command line help, %1 is the module name
    Print(
      Builtins.sformat(
        "    yast2 %1 help",
        Ops.get_string(@modulecommands, "id", "")
      )
    )
    Print(
      Builtins.sformat(
        "    yast2 %1 longhelp",
        Ops.get_string(@modulecommands, "id", "")
      )
    )
    Print(
      Builtins.sformat(
        "    yast2 %1 xmlhelp",
        Ops.get_string(@modulecommands, "id", "")
      )
    )
    # translators: module command line help, %1 is the module name
    # translate <command> only!
    Print(
      Builtins.sformat(
        _("    yast2 %1 <command> help"),
        Ops.get_string(@modulecommands, "id", "")
      )
    )
  end

  Print("")
  # translators: command line title: list of available commands
  Print(_("Commands:"))

  longest = 0
  Builtins.foreach(Ops.get_map(@modulecommands, "actions", {})) do |action, _desc|
    longest = Builtins.size(action) if Ops.greater_than(Builtins.size(action), longest)
  end

  Builtins.maplist(Ops.get_map(@modulecommands, "actions", {})) do |cmd, desc|
    if !Builtins.haskey(desc, "help")
      # translators: error message: module does not provide any help messages
      Print(
        Builtins.sformat(
          "    %1  %2",
          String.Pad(cmd, longest),
          _("No help available.")
        )
      )
    end
    if Ops.is_string?(Ops.get(desc, "help"))
      Print(
        Builtins.sformat(
          "    %1  %2",
          String.Pad(cmd, longest),
          Ops.get_string(desc, "help", "")
        )
      )
    # multiline help text
    elsif Ops.is(Ops.get(desc, "help"), "list <string>")
      help = Ops.get_list(desc, "help", [])

      if Ops.greater_than(Builtins.size(help), 0)
        Print(
          Builtins.sformat(
            "    %1  %2",
            String.Pad(cmd, longest),
            Ops.get(help, 0, "")
          )
        )
        help = Builtins.remove(help, 0)
      end

      Builtins.foreach(help) do |h|
        Print(Builtins.sformat("    %1  %2", String.Pad("", longest), h))
      end
    else
      # fallback message - invalid help has been provided by the yast module
      Print(
        Builtins.sformat(
          "    %1  %2",
          String.Pad(cmd, longest),
          _("<Error: invalid help>")
        )
      )
    end
  end
  Print("")
  if !@interactive
    # translators: module command line help, %1 is the module name
    Print(
      Builtins.sformat(
        _("Run 'yast2 %1 <command> help' for a list of available options."),
        Ops.get_string(@modulecommands, "id", "")
      )
    )
    Print("")
  end

  nil
end

#PrintHeadObject

Print a nice heading for this module



565
566
567
568
569
570
571
572
573
574
575
# File 'library/commandline/src/modules/CommandLine.rb', line 565

def PrintHead
  # translators: command line interface header, %1 is identification of the module
  head = Builtins.sformat(
    _("YaST Configuration Module %1\n"),
    Ops.get_string(@modulecommands, "id", "YaST")
  )
  head += "-" * (head.size - 1) # -1 to remove newline char from count
  head = Ops.add(Ops.add("\n", head), "\n")

  Print(head)
end

#PrintInternal(string, newline) ⇒ Object

Print a String

Print a string to /dev/tty in interactive mode, to stderr in non-interactive Suppress printing if there are no commands to be handled (starting GUI)

@param [String] string to be printed @param [Boolean] newline if newline character should be added or not



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'library/commandline/src/modules/CommandLine.rb', line 164

def PrintInternal(string, newline)
  return if !Mode.commandline

  # avoid using of uninitialized value in .dev.tty perl agent
  if string.nil?
    Builtins.y2warning("CommandLine::Print: invalid argument (nil)")
    return
  end

  if @interactive
    if newline
      SCR.Write(path(".dev.tty"), string)
    else
      SCR.Write(path(".dev.tty.nocr"), string)
    end
  elsif newline
    SCR.Write(path(".dev.tty.stderr"), string)
  else
    SCR.Write(path(".dev.tty.stderr_nocr"), string)
  end

  nil
end

#PrintNoCR(string) ⇒ Object

Print a String, don't add a trailing newline character

Print a string to /dev/tty in interactive mode, to stderr in non-interactive Suppress printing if there are no commands to be handled (starting GUI)

@param [String] string to be printed



204
205
206
# File 'library/commandline/src/modules/CommandLine.rb', line 204

def PrintNoCR(string)
  PrintInternal(string, false)
end

#PrintTable(header, content) ⇒ Object

Print a Table

Print a table using Print(). Format of table is as libyui but not all features are supported, e.g. no icons.

@param [Yast::Term] header header of table in libyui format @param [ArrayYast::Term] content content of table in libyui format



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'library/commandline/src/modules/CommandLine.rb', line 233

def PrintTable(header, content)
  header = deep_copy(header)
  content = deep_copy(content)
  aligns = []
  widths = []

  process = lambda do |line|
    line = deep_copy(line)
    ret = []
    anys = Builtins.argsof(line)
    Builtins.foreach(anys) do |a|
      if Ops.is_string?(a)
        s = Convert.to_string(a)
        ret = Builtins.add(ret, s)
      elsif Ops.is_term?(a)
        t = Convert.to_term(a)
        ret = Builtins.add(ret, Ops.get_string(Builtins.argsof(t), 0, "")) if Builtins.contains([:Left, :Center, :Right], Builtins.symbolof(t))
      end
    end
    deep_copy(ret)
  end

  get_aligns = lambda do |header2|
    header2 = deep_copy(header2)
    anys = Builtins.argsof(header2)
    Builtins.foreach(Integer.Range(Builtins.size(anys))) do |i|
      a = Ops.get(anys, i)
      if Ops.is_term?(a)
        t = Convert.to_term(a)
        Ops.set(aligns, i, :right) if Builtins.symbolof(t) == :Right
      end
    end

    nil
  end

  update_widths = lambda do |columns|
    columns = deep_copy(columns)
    Builtins.foreach(Integer.Range(Builtins.size(columns))) do |i|
      Ops.set(
        widths,
        i,
        Integer.Max(
          [Ops.get(widths, i, 0), Builtins.size(Ops.get(columns, i, ""))]
        )
      )
    end

    nil
  end

  print_line = lambda do |line|
    line = deep_copy(line)
    columns = process.call(line)
    Builtins.foreach(Integer.Range(Builtins.size(columns))) do |i|
      Ops.set(
        columns,
        i,
        String.SuperPad(
          Ops.get(columns, i, ""),
          Ops.get(widths, i, 0),
          " ",
          Ops.get(aligns, i, :left)
        )
      )
    end
    Print(Builtins.mergestring(columns, " | "))

    nil
  end

  update_widths.call(process.call(header))
  Builtins.foreach(content) { |row| update_widths.call(process.call(row)) }

  print_line.call(header)

  get_aligns.call(header)

  Print(Builtins.mergestring(Builtins.maplist(widths) do |width|
    String.Repeat("-", width)
  end, "-+-"))

  Builtins.foreach(content) { |row| print_line.call(row) }

  nil
end

#PrintVerbose(string) ⇒ Object

Same as Print(), but the string is printed only when verbose command line mode was activated

Parameters:

  • string (String)

    to print



211
212
213
214
215
# File 'library/commandline/src/modules/CommandLine.rb', line 211

def PrintVerbose(string)
  Print(string) if @verbose

  nil
end

#PrintVerboseNoCR(string) ⇒ Object

Same as PrintNoCR(), but the string is printed only when verbose command line mode was activated

Parameters:

  • string (String)

    to print



220
221
222
223
224
# File 'library/commandline/src/modules/CommandLine.rb', line 220

def PrintVerboseNoCR(string)
  PrintNoCR(string) if @verbose

  nil
end

#ProcessSystemCommands(command) ⇒ Object

Handle the system-wide commands, like help etc.

Parameters:

  • command (Hash)

    a map of the current command

Returns:

  • true, if the command was handled



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'library/commandline/src/modules/CommandLine.rb', line 876

def ProcessSystemCommands(command)
  command = deep_copy(command)
  # handle help for specific command
  # this needs to be before general help, so "help help" works
  if Ops.get(command, ["options", "help"])
    PrintHead()
    PrintActionHelp(Ops.get_string(command, "command", ""))
    return true
  end

  # Process command "interactive"
  if Ops.get_string(command, "command", "") == "interactive"
    @interactive = true
    return true
  end

  # Process command "exit"
  if Ops.get_string(command, "command", "") == "exit"
    @done = true
    @aborted = false
    return true
  end

  # Process command "abort"
  if Ops.get_string(command, "command", "") == "abort"
    @done = true
    @aborted = true
    return true
  end

  if Ops.get_string(command, "command", "") == "help"
    # don't print header when custom help is defined
    PrintHead() if !Builtins.haskey(@modulecommands, "customhelp")
    PrintGeneralHelp()
    return true
  end

  if Ops.get_string(command, "command", "") == "longhelp"
    PrintHead()
    PrintGeneralHelp()
    Builtins.foreach(Ops.get_map(@allcommands, "actions", {})) do |action, _def|
      PrintActionHelp(action)
    end
    return true
  end

  if Ops.get_string(command, "command", "") == "xmlhelp"
    if Builtins.haskey(Ops.get_map(command, "options", {}), "xmlfile") == false
      # error message - command line option xmlfile is missing
      Print(
        _(
          "Target file name ('xmlfile' option) is missing. Use xmlfile=<target_XML_file> command line option."
        )
      )
      return false
    end

    xmlfilename = Ops.get_string(command, ["options", "xmlfile"], "")

    if xmlfilename.nil? || xmlfilename == ""
      # error message - command line option xmlfile is missing
      Print(
        _(
          "Target file name ('xmlfile' option) is empty. Use xmlfile=<target_XML_file> command line option."
        )
      )
      return false
    end

    doc = {}

    #      TODO: DTD specification
    Ops.set(
      doc,
      "listEntries",
      "commands" => "command",
      "options"  => "option",
      "examples" => "example"
    )
    #      doc["cdataSections"] = [];
    Ops.set(
      doc,
      "systemID",
      Ops.add(Directory.schemadir, "/commandline.dtd")
    )
    #      doc["nameSpace"] = "http://www.suse.com/1.0/yast2ns";
    Ops.set(doc, "typeNamespace", "http://www.suse.com/1.0/configns")

    Ops.set(doc, "rootElement", "commandline")
    XML.xmlCreateDoc(:xmlhelp, doc)

    exportmap = {}
    commands = []

    actions = Ops.get_map(@cmdlinespec, "actions", {})
    mappings = Ops.get_map(@cmdlinespec, "mappings", {})
    options = Ops.get_map(@cmdlinespec, "options", {})

    Builtins.y2debug("cmdlinespec: %1", @cmdlinespec)

    Builtins.foreach(actions) do |action, description|
      help = ""
      # help text might be a simple string or a multiline text (list<string>)
      help_value = Ops.get(description, "help")
      if Ops.is_string?(help_value)
        help = Convert.to_string(help_value)
      elsif Ops.is(help_value, "list <string>")
        help = Builtins.mergestring(
          Convert.convert(
            help_value,
            from: "any",
            to:   "list <string>"
          ),
          "\n"
        )
      else
        Builtins.y2error(
          "Unsupported data type for 'help' key: %1, use 'string' or 'list<string>' type!",
          help_value
        )
      end
      opts = []
      Builtins.foreach(Ops.get(mappings, action, [])) do |option|
        optn = {
          "name" => option,
          "help" => Ops.get_string(options, [option, "help"], "")
        }
        # add type specification if it's present
        if Ops.get_string(options, [option, "type"], "") != ""
          optn = Builtins.add(
            optn,
            "type",
            Ops.get_string(options, [option, "type"], "")
          )
        end
        opts = Builtins.add(opts, optn)
      end
      actiondescr = { "help" => help, "name" => action, "options" => opts }
      # add example if it's present
      if Builtins.haskey(Ops.get(actions, action, {}), "example")
        example = Ops.get(actions, [action, "example"])
        examples = Array(example)
        actiondescr = Builtins.add(actiondescr, "examples", examples)
      end
      commands = Builtins.add(commands, actiondescr)
    end

    Ops.set(exportmap, "commands", commands)
    Ops.set(exportmap, "module", Ops.get_string(@cmdlinespec, "id", ""))

    begin
      XML.YCPToXMLFile(:xmlhelp, exportmap, xmlfilename)
    rescue XMLSerializationError => e
      # error message - creation of xml failed
      Print(
        _("Failed to create XML file.")
      )
      Builtins.y2error("Failed to serialize xml help: #{e.inspect}")
      return false
    end

    Builtins.y2milestone("exported XML map: %1", exportmap)
    return true
  end

  false
end

#Run(commandline) ⇒ Object

Parse the Command Line

Function to parse the command line, start a GUI or handle interactive and command line actions as supported by the Yast::CommandLine module.

Examples:

Complete CLI support. Methods definition are skipped for simplicity.

Yast::CommandLine.Run(
  "help"       => _("Foo Configuration"),
  "id"         => "foo",
  "guihandler" => fun_ref(method(:FooSequence), "symbol ()"),
  "initialize" => fun_ref(Foo.method(:ReadNoGUI), "boolean ()"),
  "finish"     => fun_ref(Foo.method(:WriteNoGUI), "boolean ()"),
  "actions"    => {
    "list"   => {
      "help"     => _(
        "Display configuration summary"
        ),
      "example"  => "foo list configured",
      "readonly" => true,
      "handler"  => fun_ref(
        method(:ListHandler),
        "boolean (map <string, string>)"
      )
    },
    "edit"   => {
      "help"    => _("Change existing configuration"),
      "handler" => fun_ref(
        method(:EditHandler),
        "boolean (map <string, string>)"
      )
    },
  },
  "options"    => {
    "configured"   => {
      "help" => _("List only configured foo fighters")
    }
  },
  "mappings"   => {
    "list"   => ["configured"]
  }
)

Parameters:

  • commandline (Hash)

    a map used in the CommandLine module with information about the handlers for GUI and commands.

Options Hash (commandline):

  • "help" (String)

    global help text. Help for options and actions are separated. Mandatory if module support command line.

  • "id" (String)

    module id. Mandatory if module support command line.

  • "guihandler" (Yast::FunRef("symbol ()")|Yast::FunRef("boolean ()"))

    function to be called when gui requested. Mandatory for modules with GUI.

  • "initialize" (Yast::FunRef("boolean ()"))

    function that is called before any action handler is called. Usually module initialization happens there.

  • "finish" (Yast::FunRef("boolean ()"))

    function that is called after all action handlers are called. Usually writing of changes happens there. NOTE: calling is skipped if all called handlers are readonly.

  • "actions" (Hash<String, Object>)

    definition of actions. Hash has action name as key and value is hash with following keys:

    • "help" String|Array mandatory action specific help text. Options help text is defined separately. If array is passed it will be printed indended on multiple lines.
    • "handler" Yast::FunRef("boolean (map )") handler when action is specified. Parameter is passed options. Mandatory.
    • "example" String optional example of action invocation. By default no example is provided.
    • "options" Array optional list of flags. So far only "non_strict" supported. Useful when action arguments is not well defined, so unknown option does not finish with error. By default it is empty array.
    • "readonly" Boolean optional flag that if it is set to true then invoking action is not reason to run finish handler, but if another action without readonly is called, it will run finish handler. Default value is false.
  • "options" (Hash<String, Object>)

    definition of options. Hash has action name as key and value is hash with following keys:

    • "help" String|Array mandatory action specific help text. If array is passed it will be printed indended on multiple lines.
    • "type" String optional type check for option parameter. By default no type checking is done. It aborts if no checking is done and a value is passed on CLI. Possible values are ycp types and additionally enum and regex. For enum additional key "typespec" with array of values have to be specified. For regex additional key "typespec" with string containing ycp regexp is required. For integer it does conversion of a string value to an integer value.
    • "typespec" Object additional type specification. See "type" for details.
  • "mappings" (Hash<String, Array<String>>)

    defines connection between "actions" and its "options". The key is action and the value is a list of options it supports.

Returns:

  • (Object)

    false if there was an error or there are no changes to be written (for example "help"). true if the changes should be written, or a value returned by the handler. Actions that are read-only return also true on success even if there is nothing to write.



1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
# File 'library/commandline/src/modules/CommandLine.rb', line 1530

def Run(commandline)
  commandline = deep_copy(commandline)
  # The main ()
  Builtins.y2milestone("----------------------------------------")
  Builtins.y2milestone("Command line interface started")

  # Initialize the arguments
  @done = false
  return !Aborted() if !Init(commandline, WFM.Args)

  ret = true
  # no action is readonly, but the first module without "readonly" will switch the flag to `false`
  read_only = true

  initialized = false
  if Ops.get(commandline, "initialize").nil?
    # no initialization routine
    # set initialized state to true => call finish handler at the end in command line mode
    initialized = true
  end

  # Start GUI
  if StartGUI()
    if !Builtins.haskey(commandline, "guihandler")
      Builtins.y2error(
        "Missing GUI handler for %1",
        Ops.get_string(commandline, "id", "<unknown>")
      )
      # translators: error message - the module does not provide command line interface
      Error(_("There is no user interface available for this module."))
      return false
    end

    if Ops.is(Ops.get(commandline, "guihandler"), "symbol ()")
      exec = Convert.convert(
        Ops.get(commandline, "guihandler"),
        from: "any",
        to:   "symbol ()"
      )
      symbol_ret = exec.call
      Builtins.y2debug("GUI handler ret=%1", symbol_ret)
      return symbol_ret
    else
      exec = Convert.convert(
        Ops.get(
          commandline,
          "guihandler",
          fun_ref(method(:fake_false), "boolean ()")
        ),
        from: "any",
        to:   "boolean ()"
      )
      ret = exec.call
      Builtins.y2debug("GUI handler ret=%1", ret)
      return ret
    end
  else
    # translators: progress message - command line interface ready
    PrintVerbose(_("Ready"))

    until Done()
      m = Command()
      command = Ops.get_string(m, "command", "exit")
      options = Ops.get_map(m, "options", {})

      # start initialization code if it wasn't already used
      if !initialized && (Builtins.haskey(Ops.get_map(commandline, "actions", {}), command) &&
            Ops.get(commandline, "initialize"))
        # non-GUI handling
        PrintVerbose(_("Initializing"))
        ret2 = commandline["initialize"].call
        if ret2
          initialized = true
        else
          Builtins.y2milestone("Module initialization failed")
          return false
        end
      end

      exec = Convert.convert(
        Ops.get(commandline, ["actions", command, "handler"]),
        from: "any",
        to:   "boolean (map <string, string>)"
      )

      # there is a handler, execute the action
      if !exec.nil?
        res = exec.call(options)
        # unless an action explicitly mentions that it is read-only it will run the finish handler
        read_only = false unless commandline["actions"][command]["readonly"]

        # if it is not interactive, abort on errors
        Abort() if !Interactive() && res == false
      elsif !Done()
        Builtins.y2error("Unknown command '%1' from CommandLine", command)
        next
      end
    end

    ret = !Aborted()
  end

  if ret && Ops.get(commandline, "finish") && initialized && !read_only
    # translators: Progress message - the command line interface is about to finish
    PrintVerbose(_("Finishing"))
    ret = commandline["finish"].call
    if !ret
      Builtins.y2milestone("Module finishing failed")
      return false
    end
    # translators: The command line interface is finished
    PrintVerbose(_("Done"))
  else
    # translators: The command line interface is finished without writing the changes
    PrintVerbose(_("Quitting (without changes)"))
  end

  Builtins.y2milestone("Commandline interface finished")
  Builtins.y2milestone("----------------------------------------")

  ret
end

#ScanArray<String>

Scan a command line from stdin, return it split into a list

Returns:

  • (Array<String>)

    the list of command line parts, nil for end of file



1265
1266
1267
1268
1269
1270
# File 'library/commandline/src/modules/CommandLine.rb', line 1265

def Scan
  res = Convert.to_string(SCR.Read(path(".dev.tty")))
  return nil if res.nil?

  String.ParseOptions(res, "separator" => " ")
end

#StartGUIObject

Should module start UI?

@return [Boolean] true, if the user asked for standard UI (no parameter was passed by command line)



1365
1366
1367
# File 'library/commandline/src/modules/CommandLine.rb', line 1365

def StartGUI
  !Mode.commandline
end

#UniqueOption(options, unique_options) ⇒ Object

Check uniqueness of an option

Check uniqueness of an option. Simply pass the list of user-specified options and a list of mutually exclusive options. In case of error, Report::Error is used.

Parameters:

  • options (Hash{String => String})

    options specified by the user on the command line to be checked

  • unique_options (Array)

    list of mutually exclusive options to check against

Returns:

  • nil if there is a problem, otherwise the unique option found



1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
# File 'library/commandline/src/modules/CommandLine.rb', line 1408

def UniqueOption(options, unique_options)
  return nil if options.nil? || unique_options.nil?

  # sanity check
  if unique_options.empty?
    log.error "Unique list of options required, but the list of the possible options is empty"
    return nil
  end

  # first do a filtering, then convert to a list of keys
  cmds = unique_options & options.keys

  # if it is OK, quickly return
  return cmds.first if cmds.size == 1

  msg = if cmds.empty?
    if unique_options.size == 1
      # translators: error message - missing unique command for command line execution
      Builtins.sformat(_("Specify the command '%1'."), unique_options.first)
    else
      # translators: error message - missing unique command for command line execution
      Builtins.sformat(_("Specify one of the commands: %1."), format_list(unique_options))
    end
  else
    Builtins.sformat(_("Specify only one of the commands: %1."), format_list(cmds))
  end

  Report.Error(msg)
  nil
end

#UserInput(prompt) ⇒ String

Read input from command line

Parameters:

  • prompt (String)

    Set prompt to this value

Returns:



1298
1299
1300
# File 'library/commandline/src/modules/CommandLine.rb', line 1298

def UserInput(prompt)
  GetInput(prompt, :nohistory)
end

#VerboseObject

Return verbose flag boolean verbose flag



1675
1676
1677
# File 'library/commandline/src/modules/CommandLine.rb', line 1675

def Verbose
  @verbose
end

#YesNoBoolean

Ask user, commandline equivalent of Popup::YesNo()

Returns:

  • (Boolean)

    true if user entered "yes"



1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
# File 'library/commandline/src/modules/CommandLine.rb', line 1655

def YesNo
  # prompt message displayed in the commandline mode
  # when user is asked to replay "yes" or "no" (localized)
  prompt = _("yes or no?")

  ui = UserInput(prompt)

  # yes - used in the command line mode as input text for yes/no confirmation
  yes = _("yes")

  # no - used in the command line mode as input text for yes/no confirmation
  no = _("no")

  ui = UserInput(prompt) while ui != yes && ui != no

  ui == yes
end