Method: Yast::CommandLineClass#PrintActionHelp

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

#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