Revision 552c8dff

b/lib/cli.py
44 44

  
45 45
__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain",
46 46
           "SubmitOpCode", "GetClient",
47
           "cli_option", "ikv_option", "keyval_option",
47
           "cli_option",
48 48
           "GenerateTable", "AskUser",
49 49
           "ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
50 50
           "USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT", "SUBMIT_OPT",
......
233 233
    raise OptionValueError("option %s: %s" % (opt, err))
234 234

  
235 235

  
236
class CliOption(Option):
237
  """Custom option class for optparse.
238

  
239
  """
240
  TYPES = Option.TYPES + ("unit",)
241
  TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
242
  TYPE_CHECKER["unit"] = check_unit
243

  
244

  
245 236
def _SplitKeyVal(opt, data):
246 237
  """Convert a KeyVal string into a dict.
247 238

  
......
280 271

  
281 272

  
282 273
def check_ident_key_val(option, opt, value):
283
  """Custom parser for the IdentKeyVal option type.
274
  """Custom parser for ident:key=val,key=val options.
275

  
276
  This will store the parsed values as a tuple (ident, {key: val}). As such,
277
  multiple uses of this option via action=append is possible.
284 278

  
285 279
  """
286 280
  if ":" not in value:
......
304 298
  return retval
305 299

  
306 300

  
307
class IdentKeyValOption(Option):
308
  """Custom option class for ident:key=val,key=val options.
309

  
310
  This will store the parsed values as a tuple (ident, {key: val}). As
311
  such, multiple uses of this option via action=append is possible.
312

  
313
  """
314
  TYPES = Option.TYPES + ("identkeyval",)
315
  TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
316
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
317

  
318

  
319 301
def check_key_val(option, opt, value):
320
  """Custom parser for the KeyVal option type.
302
  """Custom parser class for key=val,key=val options.
303

  
304
  This will store the parsed values as a dict {key: val}.
321 305

  
322 306
  """
323 307
  return _SplitKeyVal(opt, value)
324 308

  
325 309

  
326
class KeyValOption(Option):
327
  """Custom option class for key=val,key=val options.
328

  
329
  This will store the parsed values as a dict {key: val}.
310
class CliOption(Option):
311
  """Custom option class for optparse.
330 312

  
331 313
  """
332
  TYPES = Option.TYPES + ("keyval",)
333
  TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
314
  TYPES = Option.TYPES + (
315
    "identkeyval",
316
    "keyval",
317
    "unit",
318
    )
319
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
320
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
334 321
  TYPE_CHECKER["keyval"] = check_key_val
322
  TYPE_CHECKER["unit"] = check_unit
335 323

  
336 324

  
337 325
# optparse.py sets make_option, so we do it for our own option class, too
338 326
cli_option = CliOption
339
ikv_option = IdentKeyValOption
340
keyval_option = KeyValOption
341 327

  
342 328

  
343 329
def _ParseArgs(argv, commands, aliases):
b/scripts/gnt-backup
214 214
  make_option("-n", "--node", dest="node",
215 215
              help="Target node and optional secondary node",
216 216
              metavar="<pnode>[:<snode>]"),
217
  keyval_option("-B", "--backend", dest="beparams",
218
                type="keyval", default={},
219
                help="Backend parameters"),
217
  cli_option("-B", "--backend", dest="beparams",
218
             type="keyval", default={},
219
             help="Backend parameters"),
220 220
  make_option("-t", "--disk-template", dest="disk_template",
221 221
              help="Custom disk setup (diskless, file, plain, drbd)",
222 222
              default=None, metavar="TEMPL"),
223
  ikv_option("--disk", help="Disk information",
223
  cli_option("--disk", help="Disk information",
224 224
             default=[], dest="disks",
225 225
             action="append",
226 226
             type="identkeyval"),
......
228 228
             " single-disk configuration, when not using the --disk option,"
229 229
             " in MiB unless a suffix is used",
230 230
             default=None, type="unit", metavar="<size>"),
231
  ikv_option("--net", help="NIC information",
231
  cli_option("--net", help="NIC information",
232 232
             default=[], dest="nics",
233 233
             action="append",
234 234
             type="identkeyval"),
......
252 252
              metavar="<DIR>"),
253 253
  make_option("--file-driver", dest="file_driver", help="Driver to use"
254 254
              " for image files", default="loop", metavar="<DRIVER>"),
255
  ikv_option("-H", "--hypervisor", dest="hypervisor",
255
  cli_option("-H", "--hypervisor", dest="hypervisor",
256 256
              help="Hypervisor and hypervisor options, in the format"
257 257
              " hypervisor:option=value,option=value,...", default=None,
258 258
              type="identkeyval"),
b/scripts/gnt-cluster
583 583
                        help="Comma-separated list of hypervisors",
584 584
                        type="string",
585 585
                        default=constants.DEFAULT_ENABLED_HYPERVISOR),
586
            ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
586
            cli_option("-H", "--hypervisor-parameters", dest="hvparams",
587 587
                       help="Hypervisor and hypervisor options, in the"
588 588
                         " format"
589 589
                       " hypervisor:option=value,option=value,...",
590 590
                       default=[],
591 591
                       action="append",
592 592
                       type="identkeyval"),
593
            keyval_option("-B", "--backend-parameters", dest="beparams",
594
                          type="keyval", default={},
595
                          help="Backend parameters"),
596
            keyval_option("-N", "--nic-parameters", dest="nicparams",
597
                          type="keyval", default={},
598
                          help="NIC parameters"),
593
            cli_option("-B", "--backend-parameters", dest="beparams",
594
                       type="keyval", default={},
595
                       help="Backend parameters"),
596
            cli_option("-N", "--nic-parameters", dest="nicparams",
597
                       type="keyval", default={},
598
                       help="NIC parameters"),
599 599
            make_option("-C", "--candidate-pool-size",
600 600
                        default=constants.MASTER_POOL_SIZE_DEFAULT,
601 601
                        help="Set the candidate pool size",
......
672 672
              make_option("--enabled-hypervisors", dest="enabled_hypervisors",
673 673
                          help="Comma-separated list of hypervisors",
674 674
                          type="string", default=None),
675
              ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
675
              cli_option("-H", "--hypervisor-parameters", dest="hvparams",
676 676
                         help="Hypervisor and hypervisor options, in the"
677 677
                         " format"
678 678
                         " hypervisor:option=value,option=value,...",
679 679
                         default=[],
680 680
                         action="append",
681 681
                         type="identkeyval"),
682
              keyval_option("-B", "--backend-parameters", dest="beparams",
683
                            type="keyval", default={},
684
                            help="Backend parameters"),
685
              keyval_option("-N", "--nic-parameters", dest="nicparams",
686
                            type="keyval", default={},
687
                            help="NIC parameters"),
682
              cli_option("-B", "--backend-parameters", dest="beparams",
683
                         type="keyval", default={},
684
                         help="Backend parameters"),
685
              cli_option("-N", "--nic-parameters", dest="nicparams",
686
                         type="keyval", default={},
687
                         help="NIC parameters"),
688 688
              make_option("-C", "--candidate-pool-size", default=None,
689 689
                          help="Set the candidate pool size",
690 690
                          dest="candidate_pool_size", type="int"),
b/scripts/gnt-instance
1345 1345
              help="Target node and optional secondary node",
1346 1346
              metavar="<pnode>[:<snode>]"),
1347 1347
  os_opt,
1348
  keyval_option("-B", "--backend", dest="beparams",
1349
                type="keyval", default={},
1350
                help="Backend parameters"),
1348
  cli_option("-B", "--backend", dest="beparams",
1349
             type="keyval", default={},
1350
             help="Backend parameters"),
1351 1351
  make_option("-t", "--disk-template", dest="disk_template",
1352 1352
              help="Custom disk setup (diskless, file, plain or drbd)",
1353 1353
              default=None, metavar="TEMPL"),
......
1355 1355
             " single-disk configuration, when not using the --disk option,"
1356 1356
             " in MiB unless a suffix is used",
1357 1357
             default=None, type="unit", metavar="<size>"),
1358
  ikv_option("--disk", help="Disk information",
1358
  cli_option("--disk", help="Disk information",
1359 1359
             default=[], dest="disks",
1360 1360
             action="append",
1361 1361
             type="identkeyval"),
1362
  ikv_option("--net", help="NIC information",
1362
  cli_option("--net", help="NIC information",
1363 1363
             default=[], dest="nics",
1364 1364
             action="append",
1365 1365
             type="identkeyval"),
......
1382 1382
  make_option("-I", "--iallocator", metavar="<NAME>",
1383 1383
              help="Select nodes for the instance automatically using the"
1384 1384
              " <NAME> iallocator plugin", default=None, type="string"),
1385
  ikv_option("-H", "--hypervisor", dest="hypervisor",
1385
  cli_option("-H", "--hypervisor", dest="hypervisor",
1386 1386
              help="Hypervisor and hypervisor options, in the format"
1387 1387
              " hypervisor:option=value,option=value,...", default=None,
1388 1388
              type="identkeyval"),
......
1522 1522
                    "Replaces all disks for the instance"),
1523 1523
  'modify': (SetInstanceParams, ARGS_ONE,
1524 1524
             [DEBUG_OPT, FORCE_OPT,
1525
              keyval_option("-H", "--hypervisor", type="keyval",
1526
                            default={}, dest="hypervisor",
1527
                            help="Change hypervisor parameters"),
1528
              keyval_option("-B", "--backend", type="keyval",
1529
                            default={}, dest="beparams",
1530
                            help="Change backend parameters"),
1531
              ikv_option("--disk", help="Disk changes",
1525
              cli_option("-H", "--hypervisor", type="keyval",
1526
                         default={}, dest="hypervisor",
1527
                         help="Change hypervisor parameters"),
1528
              cli_option("-B", "--backend", type="keyval",
1529
                         default={}, dest="beparams",
1530
                         help="Change backend parameters"),
1531
              cli_option("--disk", help="Disk changes",
1532 1532
                         default=[], dest="disks",
1533 1533
                         action="append",
1534 1534
                         type="identkeyval"),
1535
              ikv_option("--net", help="NIC changes",
1535
              cli_option("--net", help="NIC changes",
1536 1536
                         default=[], dest="nics",
1537 1537
                         action="append",
1538 1538
                         type="identkeyval"),
......
1550 1550
               m_node_opt, m_pri_node_opt, m_sec_node_opt,
1551 1551
               m_clust_opt, m_inst_opt,
1552 1552
               SUBMIT_OPT,
1553
               keyval_option("-H", "--hypervisor", type="keyval",
1554
                             default={}, dest="hvparams",
1555
                             help="Temporary hypervisor parameters"),
1556
               keyval_option("-B", "--backend", type="keyval",
1557
                             default={}, dest="beparams",
1558
                             help="Temporary backend parameters"),
1553
               cli_option("-H", "--hypervisor", type="keyval",
1554
                          default={}, dest="hvparams",
1555
                          help="Temporary hypervisor parameters"),
1556
               cli_option("-B", "--backend", type="keyval",
1557
                           default={}, dest="beparams",
1558
                           help="Temporary backend parameters"),
1559 1559
               ],
1560 1560
              "<instance>", "Starts an instance"),
1561 1561

  

Also available in: Unified diff