-
Notifications
You must be signed in to change notification settings - Fork 43
/
CommandLine.rb
1697 lines (1507 loc) · 53.4 KB
/
CommandLine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
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
721
722
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
871
872
873
874
875
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
# encoding: utf-8
# ***************************************************************************
#
# Copyright (c) 2002 - 2012 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#
# ***************************************************************************
# File: modules/CommandLine.ycp
# Package: yast2
# Summary: Command line interface for YaST2 modules
# Author: Stanislav Visnovsky <visnov@suse.cz>
#
# $Id$
require "yast"
module Yast
class CommandLineClass < Module
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"
)
},
"longhelp" => {
# translators: help for 'longhelp' option on command line
"help" => _(
"Print a long version of help for this module"
)
},
"xmlhelp" => {
# translators: help for 'xmlhelp' option on command line
"help" => _(
"Print a long version of help for this module in XML format"
)
},
"interactive" => {
# translators: help for 'interactive' option on command line
"help" => _(
"Start interactive shell to control the module"
)
},
"exit" => {
# translators: help for 'exit' command line interactive mode
"help" => _(
"Exit interactive mode and save the changes"
)
},
"abort" => {
# translators: help for 'abort' command line interactive mode
"help" => _(
"Abort interactive mode without saving the changes"
)
}
},
"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
# 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] s the string to be printed
def PrintInternal(s, newline)
return if !Mode.commandline
# avoid using of uninitialized value in .dev.tty perl agent
if s.nil?
Builtins.y2warning("CommandLine::Print: invalid argument (nil)")
return
end
if @interactive
if newline
SCR.Write(path(".dev.tty"), s)
else
SCR.Write(path(".dev.tty.nocr"), s)
end
else
if newline
SCR.Write(path(".dev.tty.stderr"), s)
else
SCR.Write(path(".dev.tty.stderr_nocr"), s)
end
end
nil
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] s the string to be printed
def Print(s)
PrintInternal(s, true)
end
# 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] s the string to be printed
def PrintNoCR(s)
PrintInternal(s, false)
end
# Same as Print(), but the string is printed only when verbose command
# line mode was activated
# @param [String] s string to print
def PrintVerbose(s)
Print(s) if @verbose
nil
end
# Same as PrintNoCR(), but the string is printed only when verbose command
# line mode was activated
# @param [String] s string to print
def PrintVerboseNoCR(s)
PrintNoCR(s) if @verbose
nil
end
# 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 [Array<Yast::Term>] content content of table in libyui format
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)
if Builtins.contains([:Left, :Center, :Right], Builtins.symbolof(t))
ret = Builtins.add(ret, Ops.get_string(Builtins.argsof(t), 0, ""))
end
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
# Print an Error Message
#
# Print an error message and add the description how to get the help.
# @param [String] message error message to be printed. Use nil for no message
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
# 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{String => Object}] containing the command and it's option. In case of
# error it is an empty map.
def Parse(arguments)
arguments = deep_copy(arguments)
args = deep_copy(arguments)
return {} if Ops.less_than(Builtins.size(args), 1)
# Parse command
command = Ops.get_string(args, 0, "")
Builtins.y2debug("command=%1", command)
args = Builtins.remove(args, 0)
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 { "command" => command }
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|
if Ops.is_string?(k)
cmdoptions = Builtins.add(cmdoptions, k, Ops.get_map(allopts, k, {}))
end
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)
if Builtins.size(o) == 2
givenoptions = Builtins.add(
givenoptions,
Ops.get(o, 0, ""),
Ops.get(o, 1, "")
)
elsif Builtins.size(o) == 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
if opttype == "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
elsif opttype == "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
elsif opttype == "integer"
i = Builtins.tointeger(v)
ret = !i.nil?
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
else
# update value of the option to integer
Ops.set(givenoptions, o, i)
end
else
if v == ""
ret = false
else
ret = TypeRepository.is_a(v, opttype)
end
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
else
# type is missing
if 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
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
# Print a nice heading for this module
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")
)
headlen = Builtins.size(head)
i = 0
while Ops.less_than(i, headlen)
head = Ops.add(head, "-")
i = Ops.add(i, 1)
end
head = Ops.add(Ops.add("\n", head), "\n")
Print(head)
nil
end
# Print a help text for a given action.
#
# @param [String] action the action for which the help should be printed
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
if Ops.greater_than(Builtins.size(t), longestarg)
longestarg = Builtins.size(t)
end
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
# Print a general help - list of available command.
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, %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", "")
)
)
else
# 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")
end
Print("")
# translators: command line title: list of available commands
Print(_("Commands:"))
longest = 0
Builtins.foreach(Ops.get_map(@modulecommands, "actions", {})) do |action, _desc|
if Ops.greater_than(Builtins.size(action), longest)
longest = Builtins.size(action)
end
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
# Handle the system-wide commands, like help etc.
#
# @param [Hash] command a map of the current command
# @return true, if the command was handled
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",