Class: Kibuvits_krl171bt4_argv_parser

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kibuvits_ruby_library_krl171bt4_.rb

Overview

It’s used for placing console arguments to a hashtable.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKibuvits_krl171bt4_argv_parser



15861
15862
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 15861

def initialize
end

Class Method Details

.normalize_parsing_result(ht_normalization_specification, ht_args, msgcs = nil) ⇒ Object

normalize_parsing_result



16263
16264
16265
16266
16267
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16263

def Kibuvits_krl171bt4_argv_parser.normalize_parsing_result(
   ht_normalization_specification, ht_args, msgcs=nil)
   Kibuvits_krl171bt4_argv_parser.instance.normalize_parsing_result(
   ht_normalization_specification, ht_args,msgcs)
end

.run(ht_grammar, argv, msgcs) ⇒ Object

run



16106
16107
16108
16109
16110
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16106

def Kibuvits_krl171bt4_argv_parser.run(ht_grammar,argv,msgcs)
   ht_args=Kibuvits_krl171bt4_argv_parser.instance.run(
   ht_grammar,argv,msgcs)
   return ht_args
end

.verify_compulsory_input(s_or_ar_console_arg_name, ht_grammar, ht_args, msgcs) ⇒ Object

verify_compulsory_input



16386
16387
16388
16389
16390
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16386

def Kibuvits_krl171bt4_argv_parser.verify_compulsory_input(s_or_ar_console_arg_name,
   ht_grammar,ht_args,msgcs)
   Kibuvits_krl171bt4_argv_parser.instance.verify_compulsory_input(
   s_or_ar_console_arg_name, ht_grammar,ht_args,msgcs)
end

.verify_parsed_input(ht_grammar, ht_args, msgcs) ⇒ Object

verify_parsed_input



16357
16358
16359
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16357

def  Kibuvits_krl171bt4_argv_parser.verify_parsed_input(ht_grammar, ht_args, msgcs)
   Kibuvits_krl171bt4_argv_parser.instance.verify_parsed_input(ht_grammar,ht_args,msgcs)
end

Instance Method Details

#normalize_parsing_result(ht_normalization_specification, ht_args, msgcs = nil) ⇒ Object

The ht_args is the Kibuvits_krl171bt4_argv_parser.run output.

The idea behind the Kibuvits_krl171bt4_argv_parser.normalize_parsing_result is that if there are multiple console arguments that actually are synonyms, for example, “-f”, “–file”, “–files”, then to simplify the code that has to act upon the console arguments, it makes sense to converge the values. For example, in the case of the “-f”, “–file”, “–files” one might want to collect all of the file paths to a single array that resides in one certain location in the ht_args, let’s say next to “–file”. In that case, the ht_normalization_specification has “–file” as its key and an array of its synonyms as the value that corresponds to the “–file”: [“-f”,“–files”].

It’s OK to place the keys of the ht_normalization_specification to the array of values, but it won’t change anything, because a thing x is considered to be its own synonym.

Since the normalizing changes the ht_args content, the numbers of the console arguments should be verified prior to normalization.



16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16186

def normalize_parsing_result(ht_normalization_specification, ht_args,
   msgcs=nil)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, Hash, ht_normalization_specification
      kibuvits_krl171bt4_typecheck bn, Hash, ht_args
      kibuvits_krl171bt4_typecheck bn, [Kibuvits_krl171bt4_msgc_stack,NilClass],msgcs
   end # if
   ht_spec=ht_normalization_specification
   if ht_spec.length==0
      kibuvits_krl171bt4_throw "ht_normalization_specification.length==0 does "+
      "not make sense."
   end # if
   ar_converged=nil
   ar_args=nil
   b_ar_synonyms_contains_s_target=false
   x=nil
   ht_spec.each_pair do |s_target,ar_synonyms| # == |key,value|
      if !ht_args.has_key? s_target
         kibuvits_krl171bt4_throw "ht_args does not have a key of \""+s_target+
         "\", but the key is present in the "+
         "ht_normalization_specification."
      end # if
      if ar_synonyms.class!=Array
         kibuvits_krl171bt4_throw "ht_normalization_specification['"+s_target+
         "'].class!=Array ."
      end # if
      if s_target.class!=String
         kibuvits_krl171bt4_throw "ht_normalization_specification keys are "+
         "expected to be strings, but a value of type "+
         s_target.class.to_s+" was found.\ns_target=="+
         s_target.to_s
      end # if
      normalize_parsing_result_verify_ar_synonyms(
      ar_synonyms,s_target,msgcs)
      ar_converged=nil
      ar_synonyms.each do |s_synonym|
         b_ar_synonyms_contains_s_target=true if s_synonym==s_target
         if !ht_args.has_key? s_synonym
            kibuvits_krl171bt4_throw "ht_normalization_specification['"+s_target+"']"+
            " contains element \""+s_synonym+"\", which is "+
            "not among the keys of the ht_args. "
         end # if
         if s_synonym.class!=String
            kibuvits_krl171bt4_throw "s_synonym.class=="+s_synonym.class.to_s+
            ", but String is expected.\ns_synonym=="+s_synonym.to_s
         end # if
         ar_args=ht_args[s_synonym]
         if ar_args!=nil
            normalize_parsing_result_verify_ar_synonyms(
            ar_args,s_target,msgcs)
            normalize_parsing_result_verify_ar_args_elem_type(
            ar_args, s_target, true)
            ar_converged=Array.new if ar_converged==nil
            ar_converged=ar_converged+ar_args
         end # if
      end # loop
      if !b_ar_synonyms_contains_s_target
         if !ht_args.has_key? s_target
            kibuvits_krl171bt4_throw "A key of the ht_normalization_specification, "+
            "'"+s_target+"', is not among the keys of the ht_args. "
         end # if
         ar_args=ht_args[s_target]
         if ar_args!=nil
            normalize_parsing_result_verify_ar_synonyms(
            ar_args,s_target,msgcs)
            normalize_parsing_result_verify_ar_args_elem_type(
            ar_args, s_target, false)
            ar_converged=Array.new if ar_converged==nil
            ar_converged=ar_converged+ar_args
         end # if
      end # if
      ar_converged=ar_converged.uniq if ar_converged!=nil
      ht_args[s_target]=ar_converged
   end # loop
end

#normalize_parsing_result_verify_ar_args_elem_type(ar_args, s_target, b_ht_args) ⇒ Object

normalize_parsing_result_verify_ar_synonyms



16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16145

def normalize_parsing_result_verify_ar_args_elem_type(
   ar_args, s_target, b_ht_args)
   ar_args.each do |x|
      if x.class!=String
         if b_ht_args
            kibuvits_krl171bt4_throw "The array that is stored to "+
            "ht_args['"+s_target+"'] had a non-string element "+
            "with the value of "+x.to_s+" ."
         else
            kibuvits_krl171bt4_throw "The array that is stored to "+
            "ht_normalization_specification['"+
            s_target+"'] had a non-string element "+
            "with the value of "+x.to_s+" ."
         end # if
      end # if
   end # loop
end

#normalize_parsing_result_verify_ar_synonyms(ar_synonyms, s_target, msgcs) ⇒ Object



16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16114

def normalize_parsing_result_verify_ar_synonyms(
   ar_synonyms, s_target,msgcs)
   x=ar_synonyms.uniq
   if msgcs!=nil
      if KIBUVITS_krl171bt4_b_DEBUG
         #kibuvits_krl171bt4_writeln "\nx=="+Kibuvits_krl171bt4_str.array2xseparated_list(x)+
         #"\nar_synonyms=="+
         #Kibuvits_krl171bt4_str.array2xseparated_list(ar_synonyms)+"\n"+
         #"s_target=="+s_target+"\n"
      end # if
   end # if
   return if x.length==ar_synonyms.length
   ht_duplicates=Hash.new
   ar_synonyms.each do |s_synonym|
      if !ht_duplicates.has_key? s_synonym
         ht_duplicates[s_synonym]=1
      else
         ht_duplicates[s_synonym]=1+ht_duplicates[s_synonym]
      end # if
   end # loop
   ar_duplicates=Array.new
   ht_duplicates.each_pair do |a_key,a_value|
      ar_duplicates<<a_key if 1<a_value
   end # loop
   s_duplicates=Kibuvits_krl171bt4_str.array2xseparated_list(ar_duplicates)
   kibuvits_krl171bt4_throw "ar_synonyms.uniq.length=="+x.length.to_s+
   "!=ar_synonyms.length=="+ar_synonyms.length.to_s+
   " for ht_normalization_specification['"+s_target+"']. "+
   "The duplicates are: "+s_duplicates+". "
end

#run(ht_grammar, argv, msgcs) ⇒ Object

The key of the ht_grammar is expected to be a string that matches a regex of ^[-][wd-]* For example, “–file-name”, “-f”, “-o”, “-42” and “-x” match that regex.

The values of the ht_grammar are expected to be an integers that are within a set that is an union of natural numbers and -1,0 The value that is greater than or equal to 0 indicates the number of expected arguments after the argument specification. For example, ht_grammar=1 means that exactly one console argument is expected after the “–file-name”. (-1) indicates infinity. For example, ht_grammar=(-1) means that at least one file name is expected right after the “–file-name”

The parsing considers the string that succeeds the equals sign (“=”) as one of the console arguments. So for ht_grammar=1 the “-f hi_there.txt” and “-f=hi_there.txt” are both valid.

If the ht_grammar=4 and there is no “–great” within the console args, no exception is thrown and “–great” is marked as nil in the output hashtable.

The returned hashtable, ht_args, is a copy of the input ht_grammar, except that each of the values is either nil or an array.

TODO: Add mode (-2), like the (-1), to this method and the rest of the related meothds. The (-2) would represent the “zero or more” option.

TODO: If “–help” and “–abi” have the same meaning, then the implementation should forbid them to be defined simultaniously. It should not be allowed to write “myapp -? –help –abi”, not to mention “myapp –file ./foo –sisendfail ./bar ”



16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16067

def run ht_grammar, argv, msgcs
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, Hash, ht_grammar
      kibuvits_krl171bt4_typecheck bn, Array, argv
      kibuvits_krl171bt4_typecheck bn, Kibuvits_krl171bt4_msgc_stack, msgcs
   end # if
   kibuvits_krl171bt4_throw "ht_grammar is empty." unless 0<ht_grammar.keys.length
   ht_args=init_ht_args ht_grammar
   return ht_args if msgcs.b_failure
   msgc=Kibuvits_krl171bt4_msgc.new
   msgc.b_failure=false
   msgcs<<msgc
   if argv.length==0# == <no arguments given>
      s="argv.length==0"
      msgc['English']=s
      msgc['Estonian']=s
      return ht_args
   end # if
   ht_opmem=Hash.new
   ht_opmem['var_name']=nil
   ht_opmem['ar_values']=nil
   ht_opmem['i_n_left2read']=nil
   ht_opmem['ht_grammar']=ht_grammar
   ht_opmem['ar_s_arg_split']=["",""]
   ht_opmem['ht_args']=ht_args
   ht_opmem['msgc']=msgc
   s_arg=""
   (argv.length).times do |i|
      s_arg=argv[i]
      verify_s_arg s_arg,ht_opmem
      break if msgcs.b_failure
      parse_step s_arg,ht_opmem
      break if msgcs.b_failure
   end # loop
   console_var_2_results ht_opmem unless msgcs.b_failure
   return ht_args
end

#verify_compulsory_input(s_or_ar_console_arg_name, ht_grammar, ht_args, msgcs) ⇒ Object



16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16362

def verify_compulsory_input(s_or_ar_console_arg_name,
   ht_grammar,ht_args,msgcs)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding();
      kibuvits_krl171bt4_typecheck bn, [String,Array], s_or_ar_console_arg_name
      kibuvits_krl171bt4_typecheck bn, Hash, ht_grammar
      kibuvits_krl171bt4_typecheck bn, Hash, ht_args
      kibuvits_krl171bt4_typecheck bn, Kibuvits_krl171bt4_msgc_stack, msgcs
   end # if
   ar_compulsory_args=Kibuvits_krl171bt4_ix.normalize2array(
   s_or_ar_console_arg_name)
   i_count=nil
   ar_compulsory_args.each do |s_arg|
      if !ht_grammar.has_key? s_arg
         kibuvits_krl171bt4_throw "ht_grammar does not have a key that equals "+
         "with \""+s_arg.to_s+"\"."
      end # if
      i_count=ht_grammar[s_arg]
      verify_parsed_input_verification_step(s_arg,i_count,
      ht_args,msgcs,true)
      break if msgcs.b_failure
   end # loop
end

#verify_parsed_input(ht_grammar, ht_args, msgcs) ⇒ Object

Checks whether the values of the ht_args comply with the ht_grammar. In case of miscompliance, outputs an error to the magcs



16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16343

def  verify_parsed_input(ht_grammar, ht_args, msgcs)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding();
      kibuvits_krl171bt4_typecheck bn, Hash, ht_grammar
      kibuvits_krl171bt4_typecheck bn, Hash, ht_args
      kibuvits_krl171bt4_typecheck bn, Kibuvits_krl171bt4_msgc_stack, msgcs
   end # if
   ht_grammar.each_pair do |s_arg,i_count|
      verify_parsed_input_verification_step(s_arg,i_count,
      ht_args,msgcs,false)
      break if msgcs.b_failure
   end # loop
end

#verify_parsed_input_verification_step(s_arg, i_count, ht_args, msgcs, b_assume_args_to_be_all_compulsory = false) ⇒ Object

Kibuvits_krl171bt4_argv_parser.normalize_parsing_result



16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 16269

def verify_parsed_input_verification_step(s_arg,i_count,ht_args,
   msgcs, b_assume_args_to_be_all_compulsory=false)
   bn=nil
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck(bn, String, s_arg,
      "The s_arg is a key of the ht_grammar.")
      kibuvits_krl171bt4_typecheck(bn, Fixnum, i_count,
      "The i_count==ht_grammar['"+s_arg+"'].")
      kibuvits_krl171bt4_typecheck(bn, Hash, ht_args)
   end # if
   if !ht_args.has_key? s_arg
      kibuvits_krl171bt4_throw "ht_args does not have a key that equals with \""+
      s_arg+"\"."
   end # if
   # The msgcs instance passed the typecheck in the parent function.
   ar_args=ht_args[s_arg]
   if KIBUVITS_krl171bt4_b_DEBUG
      kibuvits_krl171bt4_typecheck(bn, [Array,NilClass], ar_args,
      "The ar_args is the value of ht_args['"+s_arg+"'].")
   end # if
   if b_assume_args_to_be_all_compulsory
      if ar_args==nil # == <console argument is missing>
         msgcs.cre "Console argument "+s_arg+
         " is compulsory, but it is missing.", 4.to_s
         msgcs.last['Estonian']="Konsooliargument nimega "+s_arg+
         " on kohustuslik, kuid puudub."
         return
      end # if
   end # if
   # One assumes that console arguments are voluntary, except
   # that when they are given, they must have the right
   # number of values.
   return if ar_args==nil # == <console argument is missing>
   i_ar_argslen=ar_args.length
   case i_count
   when 0
      if i_ar_argslen!=0
         msgcs.cre "Console argument "+s_arg+" was given "+
         i_ar_argslen.to_s+"values, but it is required to "+
         "be given at most 0 values.", 1.to_s
         msgcs.last['Estonian']="Konsooliargumendile nimega "+s_arg+
         "anti "+i_ar_argslen.to_s+" väärtust, kuid on "+
         "nõutud, et talle antakse maksimaalselt 0 väärtust."
         return
      end # if
   when (-1)
      if i_ar_argslen<1
         msgcs.cre "Console argument "+s_arg+" was given "+
         "0 values, but it is required to "+
         "be given at least one value.", 2.to_s
         msgcs.last['Estonian']="Konsooliargumendile nimega "+s_arg+
         "anti 0 väärtust, kuid on õutud, et "+
         "talle antakse vähemalt üks väärtus."
         return
      end # if
   else
      if i_ar_argslen!=i_count
         msgcs.cre "Console argument "+s_arg+" was given "+
         i_ar_argslen.to_s+"values, but it is required to "+
         "be given at "+i_count.to_s+" values.", 3.to_s
         msgcs.last['Estonian']="Konsooliargumendile nimega "+s_arg+
         "anti "+i_ar_argslen.to_s+" väärtust, kuid on õutud, et "+
         "talle antakse "+i_count.to_s+" väärtust."
         return
      end # if
   end # case
end