Login
arg-types.cr at tip
Login

File src/remilib/args/arg-types.cr from the latest check-in


     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
#### libremiliacr
#### Copyright(C) 2020-2024 Remilia Scarlet <remilia@posteo.jp>
####
#### This program is free software: you can redistribute it and/or modify it
#### under the terms of the GNU General Public License as published the Free
#### Software Foundation, either version 3 of the License, or (at your option)
#### any later version.
####
#### 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, see<http:####www.gnu.org/licenses/.>
require "../strings"

module RemiLib::Args
  # The base class for all Argument types for `ArgParser`.
  abstract class Argument
    # The full argument name.  This is always prefixed with `"--"`.
    getter longName : String = ""

    # An `ArgCallbackFunc` that will be called at the end of parsing if the
    # argument was called.
    property callback : ArgCallbackFunc | Nil

    # Returns `true` if the argument was called after parsing, or `false`
    # otherwise.
    getter? called : Bool = false

    # :nodoc:
    protected setter called

    # The single `Char` name of the argument, or `nil`.  For example, if an
    # argument has the names `--foo` and '-f`, then the `#longName` is `"--foo"`
    # and the `#shortName` is `f`.
    property shortName : Char?

    # The argument group this Argument is part of.  This is mainly used for help
    # printing.
    property group : String

    # The help string for this argument, as shown during help printing.
    property help : String

    # Initializes a new `Argument` subclass.  The `#longName` is always
    # required.  Preprending a `"--"` prefix to the long name here is optional -
    # if you don't add it yourself, it will be prepended for you by this method.
    def initialize(newLongName, @shortName : Char? = nil, @group : String = "", @help : String = "")
      raise "Argument long names cannot be blank" if newLongName.strip.empty?

      if newLongName.starts_with?("--")
        @longName = newLongName.downcase
      else
        @longName = "--#{newLongName}".downcase
      end

      @callback = nil
    end

    # Sets the long name of the argument.  For example, if an argument has the
    # names `--foo` and '-f`, then the `#longName` is `"--foo"` and the
    # `#shortName` is `f`.
    #
    # If you do not include a prefix of `"--"`, it will be prepended for you.
    @[AlwaysInline]
    def longName=(newName : String) : Nil
      newName = newName.strip
      if newName.starts_with?("--")
        @longName = newName.downcase
      else
        @longName = "--#{newName}".downcase
      end
    end

    # :nodoc:
    abstract def str : String
  end

  module ValArgument
    abstract def value
    abstract def value=(val) : Nil
    abstract def setValue!(val) : Nil
  end

  module MultiArgument
    property times : Int32 = 0
  end

  module MultiValArgument
    property times : Int32 = 0
    abstract def values
    abstract def values=(vals) : Nil
    abstract def setValues!(vals) : Nil
    abstract def <<(newValue)
  end

  # A simple flag argument that always defaults to "off" and can be turned "on".
  # If it's found on the command line, it's considered `#called`.
  class FlagArgument < Argument
    # Returns `"true"` if the argument was `#called`, or `"false"` otherwise.
    def str : String
      @called.to_s
    end
  end

  # Similar to a `FlagArgument`, except that this can be called multiple times.
  class MultiFlagArgument < Argument
    include MultiArgument

    # Returns the number of `#times` this argument was called as a String.
    def str : String
      @times.to_s
    end
  end

  # An `Argument` that expects some sort of string value.
  class StringArgument < Argument
    include ValArgument
    @value : String = ""

    # A list of values that the argument is allowed to take.  If the
    # user provides a value that does not match any of these, an
    # `ArgumentError` is raised during parsing.
    property oneOf : Array(String) = [] of String

    # Sets the value of the argument.  This also sets `#called?` to `true`.
    @[AlwaysInline]
    def value=(val : String)
      if !oneOf.empty? && !oneOf.includes?(val)
        raise ArgumentError.new("#{longName} expects one of the following: #{@oneOf.join(", ")}")
      end
      @value = val
      @called = true
    end

    # :ditto:
    @[AlwaysInline]
    def value=(val) : Nil
      self.value = val.to_s
    end

    # Sets the value of the argument.  This does not set `#called?` to `true`,
    # and does not check `#oneOf`.
    @[AlwaysInline]
    def setValue!(val : String) : Nil
      @value = val
    end

    # :inherit:
    @[AlwaysInline]
    def setValue!(val) : Nil
      @value = val.to_s
    end

    # Returns the value of the argument.
    @[AlwaysInline]
    def value : String
      @value
    end

    # Returns the value of the argument.  This is interchangeable with `#value`
    # for the `StringArgument` class.
    @[AlwaysInline]
    def str : String
      @value
    end
  end

  # Similar to a `StringArgument`, except that it can be called multiple times.
  class MultiStringArgument < Argument
    include MultiValArgument
    @values = [] of String

    # A list of values that the argument is allowed to take.  If the
    # user provides a value that does not match any of these, an
    # `ArgumentError` is raised during parsing.
    property oneOf : Array(String) = [] of String

    # Appends a new value to the argument.  This also sets `#called` to `true`,
    # and increases `#times` by one.
    @[AlwaysInline]
    def <<(newVal : String)
      if !oneOf.empty? && !oneOf.includes?(newVal)
        raise ArgumentError.new("#{longName} expects one of the following: #{@oneOf.join(", ")}")
      end
      @values << newVal
      @called = true
      @times += 1
      self
    end

    # :ditto:
    @[AlwaysInline]
    def <<(newValue)
      self << val.to_s
    end

    # Sets the value of this argument.  This also sets `#called` to `true` and
    # sets `#times` to the length of `newVal`.
    @[AlwaysInline]
    def values=(vals : Array(String)) : Nil
      if !oneOf.empty? && !(vals.all? { |str| oneOf.includes?(str) })
        raise ArgumentError.new("#{longName} expects one of the following: #{@oneOf.join(", ")}")
      end
      @values = vals
      @called = true
      @times = vals.size
    end

    # :ditto:
    @[AlwaysInline]
    def values=(vals) : Nil
      self.values = vals.as(Array(String))
    end

    # Sets the value of this argument.  This does not set `#called` to `true`,
    # but does set `#times` to the length of `newVal`.  This does not check the
    # value against `#oneOf`.
    @[AlwaysInline]
    def setValues!(newVal : Array(String)) : Nil
      @values = newVal
    end

    # :ditto:
    @[AlwaysInline]
    def setValues!(vals) : Nil
      setValues!(vals.as(Array(String)))
    end

    # Returns all values stored in this argument.
    @[AlwaysInline]
    def values
      @values
    end

    # Returns all values stored in this argument as a string.
    @[AlwaysInline]
    def str : String
      @values.to_s
    end
  end

  # An `Argument` that expects an integer.  This always stores its value as an
  # `Int64`.
  class IntArgument < Argument
    include ValArgument
    @value : Int64 = 0u64

    # The minimum accepted value for this argument.
    property minimum : Int64 = Int64::MIN

    # The maximum accepted value for this argument.
    property maximum : Int64 = Int64::MAX

    # Sets the value of the argument.  This is checked against the `#minimum`
    # and `#maximum`.  This sets `#called?` to `true`.
    @[AlwaysInline]
    def value=(val : Int64) : Nil
      unless val >= @minimum && val <= @maximum
        raise ArgumentError.new("#{longName} expects an integer between #{minimum} and #{maximum}")
      end
      @value = val
      @called = true
    end

    # Sets the value of the argument.  This is checked against the `#minimum`
    # and `#maximum`.  This sets `#called?` to `true`.
    #
    # `newVal` must respond to `#to_i64`.
    @[AlwaysInline]
    def value=(val) : Nil
      if val.responds_to?(:to_i64)
        begin
          self.value = val.to_i64
        rescue Exception
          raise ArgumentError.new("#{longName} expects an integer between #{minimum} and #{maximum})")
        end
      else
        raise "Cannot assign value to an IntArgument that does not respond to #to_i64"
      end
    end

    # Sets the value of the argument.  This does not set `#called?` to `true`,
    # and does not check the `#minimum` and `#maximum`.
    @[AlwaysInline]
    def setValue!(val : Int64) : Nil
      @value = val.to_i64!
    end

    # Sets the value of the argument.  This does not set `#called?` to `true`,
    # and does not check the `#minimum` and `#maximum`.
    #
    # `val` must respond to `#to_i64`.
    @[AlwaysInline]
    def setValue!(val) : Nil
      if val.responds_to?(:to_i64)
        begin
          setValue!(val.to_i64)
        rescue Exception
          raise ArgumentError.new("#{longName} expects an integer between #{minimum} and #{maximum})")
        end
      else
        raise "Cannot assign value to an IntArgument that does not respond to #to_i64"
      end
    end

    # Returns the value of this argument.
    def value : Int64
      @value
    end

    # Returns the value of this argument as a string.
    def str : String
      @value.to_s
    end
  end

  # Similar to an `IntArgument`, except that it can be called multiple times.
  class MultiIntArgument < Argument
    include MultiValArgument
    @values = [] of Int64

    # The minimum for all accepted values for this argument.
    property minimum : Int64 = Int64::MIN

    # The maximum for all accepted values for this argument.
    property maximum : Int64 = Int64::MAX

    # Appends `val` to this argument's values.  `val` will be validated
    # against `#constraint` first.  This sets `#called` to `true`, and increases
    # `#times` by one.
    @[AlwaysInline]
    def <<(val : Int64)
      unless val >= @minimum && val <= @maximum
        raise ArgumentError.new("#{longName} expects integers between #{minimum} and #{maximum}")
      end
      @values << val.to_i64!
      @called = true
      @times += 1
      self
    end

    # Appends `newVal` to this argument's values.  `newVal` will be validated
    # against `#constraint` first.  This sets `#called` to `true`, and increases
    # `#times` by one.
    #
    # `newValue` must respond to `#to_i64`.
    @[AlwaysInline]
    def <<(newValue)
      if newValue.responds_to?(:to_i64)
        begin
          self.<< newValue.to_i64
        rescue Exception
          raise ArgumentError.new("#{longName} expects an integer between #{minimum} and #{maximum})")
        end
      else
        raise "Cannot assign a value to a MultiIntArgument that does not respond to #to_i64"
      end
    end

    # Sets all of the values for this argument in one go.  All arguments will be
    # checked against the minimum and maximum.  This sets `#called?` to `true`,
    # and sets `#times`.
    @[AlwaysInline]
    def values=(vals : Array(Int64)) : Nil
      unless vals.all? { |val| val >= @minimum && val <= @maximum }
        raise ArgumentError.new("#{longName} expects integers between #{minimum} and #{maximum}")
      end
      @values = vals
      @called = true
      @times = vals.size
    end

    # :ditto:
    @[AlwaysInline]
    def values=(vals) : Nil
      self.values = vals.as(Array(Int64))
    end

    # Sets all of the values for this argument in one go.  All arguments will be
    # checked against the minimum and maximum.  This does not set `#called?` to
    # `true`, but does set `#times`.  It does not check `#minimum` and
    # `#maximum`.
    def setValues!(vals : Array(Int64)) : Nil
      @values = vals
    end

    # :ditto:
    @[AlwaysInline]
    def setValues!(vals) : Nil
      self.values! = vals.as(Array(Int64))
    end

    # Returns the values stored in this argument.
    @[AlwaysInline]
    def values
      @values
    end

    # Returns all values stored in this argument as a string.
    @[AlwaysInline]
    def str : String
      @values.to_s
    end
  end

  # An `Argument` that expects a floating point number.  This always stores its
  # value as a `Float64`.
  class FloatArgument < Argument
    include ValArgument
    @value : Float64 = 0.0f64

    # The minimum accepted value for this argument.
    property minimum : Float64 = Float64::MIN

    # The maximum accepted value for this argument.
    property maximum : Float64 = Float64::MAX

    # Sets the value of the argument.  This is checked against the `#minimum`
    # and `#maximum`.  This sets `#called?` to `true`.
    @[AlwaysInline]
    def value=(val : Float64) : Nil
      unless val >= @minimum && val <= @maximum
        raise ArgumentError.new("#{longName} expects a number between #{minimum} and #{maximum}")
      end
      @value = val
      @called = true
    end

    # Sets the value of the argument.  This is checked against the `#minimum`
    # and `#maximum`.  This sets `#called?` to `true`.
    #
    # `val` must respond to `#to_f`.
    @[AlwaysInline]
    def value=(val) : Nil
      if val.responds_to?(:to_f64)
        begin
          self.value = val.to_f64
        rescue Exception
          raise ArgumentError.new("#{longName} expects a number between #{minimum} and #{maximum})")
        end
        @called = true
      else
        raise "Cannot assign a value to a FloatArgument that does not respond to #to_f"
      end
    end

    # Sets the value of the argument.  This is checked against the `#minimum`
    # and `#maximum`.  This does not set `#called?` to `true`.
    @[AlwaysInline]
    def setValue!(newVal : Float64) : Nil
      @value = newVal.to_f64!
    end

    # Sets the value of the argument.  This is checked against the `#minimum`
    # and `#maximum`.  This does not set `#called?` to `true`.
    #
    # `newVal` must respond to `#to_f`.
    @[AlwaysInline]
    def setValue!(val) : Nil
      if val.responds_to?(:to_f)
        begin
          self.setValue!(val.to_f.to_f64)
        rescue Exception
          raise ArgumentError.new("#{longName} expects a number between #{minimum} and #{maximum})")
        end
      else
        raise "Cannot assign a value to a FloatArgument that does not respond to #to_f"
      end
    end

    # Returns this argument's value.
    def value : Float64
      @value
    end

    # Returns the value of this argument as a string.
    def str : String
      @value.to_s
    end
  end

  # Similar to a `FloatArgument`, except that it can be called multiple times.
  class MultiFloatArgument < Argument
    include MultiValArgument
    @values = [] of Float64

    # The minimum for all accepted values for this argument.
    property minimum : Float64 = Float64::MIN

    # The maximum for all accepted values for this argument.
    property maximum : Float64 = Float64::MAX

    # Appends `newVal` to this argument's values.  `newVal` will be validated
    # against `#constraint` first.  This sets `#called` to `true`, and increases
    # `#times` by one.
    @[AlwaysInline]
    def <<(val : Float64)
      unless val >= @minimum && val <= @maximum
        raise ArgumentError.new("#{longName} expects numbers between #{minimum} and #{maximum}")
      end
      @values << val.to_f64!
      @called = true
      @times += 1
      self
    end

    # Appends `newVal` to this argument's values.  `newVal` will be validated
    # against `#constraint` first.  This sets `#called` to `true`, and increases
    # `#times` by one.
    #
    # `newValue` must respond to `#to_f`.
    @[AlwaysInline]
    def <<(newValue)
      if newValue.responds_to?(:to_f64)
        begin
          self.<< newValue.to_f64
        rescue Exception
          raise ArgumentError.new("#{longName} expects a number between #{minimum} and #{maximum})")
        end
      else
        raise "Cannot assign a value to a MultiFloatArgument that does not respond to #to_f"
      end
    end

    # Sets all of the values for this argument in one go.  All arguments will be
    # checked against the minimum and maximum.  This sets `#called?` to `true`,
    # and sets `#times`.
    @[AlwaysInline]
    def values=(vals : Array(Float64)) : Nil
      unless vals.all? { |val| val >= @minimum && val <= @maximum }
        raise ArgumentError.new("#{longName} expects numbers between #{minimum} and #{maximum}")
      end
      @values = vals
      @called = true
      @times = vals.size
    end

    # :ditto:
    @[AlwaysInline]
    def values=(vals) : Nil
      self.values = vals.as(Array(Float64))
    end

    # Sets all of the values for this argument in one go.  All arguments will be
    # checked against the minimum and maximum.  This does not set `#called?` to
    # `true` and does not set `#times`.
    def setValues!(vals : Array(Float64)) : Nil
      @values = vals
    end

    # :ditto:
    @[AlwaysInline]
    def setValues!(vals) : Nil
      self.setValues!(vals.as(Array(Float64)))
    end

    # Returns the values stored in this argument.
    def values
      @values
    end

    # Returns all values stored in this argument as a string.
    def str : String
      @values.to_s
    end
  end
end