comparison frontends/src/jp/base.py @ 2547:2d69a0afe039

jp: new argument pubsub_default can be used in Commands to replace default value in help string
author Goffi <goffi@goffi.org>
date Sat, 31 Mar 2018 17:10:43 +0200
parents dcc77f23e370
children 1d754bc14381
comparison
equal deleted inserted replaced
2546:48820e4a1f8a 2547:2d69a0afe039
322 draft_group = draft_parent.add_argument_group(_('draft handling')) 322 draft_group = draft_parent.add_argument_group(_('draft handling'))
323 draft_group.add_argument("-D", "--current", action="store_true", help=_(u"load current draft")) 323 draft_group.add_argument("-D", "--current", action="store_true", help=_(u"load current draft"))
324 draft_group.add_argument("-F", "--draft-path", type=unicode_decoder, help=_(u"path to a draft file to retrieve")) 324 draft_group.add_argument("-F", "--draft-path", type=unicode_decoder, help=_(u"path to a draft file to retrieve"))
325 325
326 326
327 def make_pubsub_group(self, flags): 327 def make_pubsub_group(self, flags, defaults):
328 """generate pubsub options according to flags 328 """generate pubsub options according to flags
329 329
330 @param flags(iterable[unicode]): see [CommandBase.__init__] 330 @param flags(iterable[unicode]): see [CommandBase.__init__]
331 @param defaults(dict[unicode, unicode]): help text for default value
332 key can be "service" or "node"
333 value will be set in " (DEFAULT: {value})", or can be None to remove DEFAULT
331 @return (ArgumentParser): parser to add 334 @return (ArgumentParser): parser to add
332 """ 335 """
333 flags = misc.FlagsHandler(flags) 336 flags = misc.FlagsHandler(flags)
334 parent = argparse.ArgumentParser(add_help=False) 337 parent = argparse.ArgumentParser(add_help=False)
335 pubsub_group = parent.add_argument_group('pubsub') 338 pubsub_group = parent.add_argument_group('pubsub')
336 pubsub_group.add_argument("-u", "--pubsub-url", type=unicode_decoder, 339 pubsub_group.add_argument("-u", "--pubsub-url", type=unicode_decoder,
337 help=_(u"Pubsub URL (xmpp or http)")) 340 help=_(u"Pubsub URL (xmpp or http)"))
338 341
339 service_help = _(u"JID of the PubSub service") 342 service_help = _(u"JID of the PubSub service")
340 if not flags.service: 343 if not flags.service:
341 service_help += _(u" (default: PEP service)") 344 default = defaults.pop(u'service', _(u'PEP service'))
345 if default is not None:
346 service_help += _(u" (DEFAULT: {default})".format(default=default))
342 pubsub_group.add_argument("-s", "--service", type=unicode_decoder, default=u'', 347 pubsub_group.add_argument("-s", "--service", type=unicode_decoder, default=u'',
343 help=service_help) 348 help=service_help)
344 349
345 node_help = _(u"node to request") 350 node_help = _(u"node to request")
346 if not flags.node: 351 if not flags.node:
347 node_help += _(u" (DEFAULT: standard node)") 352 default = defaults.pop(u'node', _(u'standard node'))
353 if default is not None:
354 node_help += _(u" (DEFAULT: {default})".format(default=default))
348 pubsub_group.add_argument("-n", "--node", type=unicode_decoder, default=u'', help=node_help) 355 pubsub_group.add_argument("-n", "--node", type=unicode_decoder, default=u'', help=node_help)
349 356
350 if flags.single_item: 357 if flags.single_item:
351 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, help=_(u"item to retrieve")) 358 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, help=_(u"item to retrieve"))
352 pubsub_group.add_argument("-L", "--last-item", action='store_true', help=_(u'retrieve last item')) 359 pubsub_group.add_argument("-L", "--last-item", action='store_true', help=_(u'retrieve last item'))
357 pubsub_group.add_argument("-m", "--max", type=int, default=10, 364 pubsub_group.add_argument("-m", "--max", type=int, default=10,
358 help=_(u"maximum number of items to get ({no_limit} to get all items)".format(no_limit=C.NO_LIMIT))) 365 help=_(u"maximum number of items to get ({no_limit} to get all items)".format(no_limit=C.NO_LIMIT)))
359 366
360 if flags: 367 if flags:
361 raise exceptions.InternalError('unknowns flags: {flags}'.format(flags=u', '.join(flags))) 368 raise exceptions.InternalError('unknowns flags: {flags}'.format(flags=u', '.join(flags)))
369 if defaults:
370 raise exceptions.InternalError('unused defaults: {defaults}'.format(defaults=defaults))
362 371
363 return parent 372 return parent
364 373
365 def add_parser_options(self): 374 def add_parser_options(self):
366 self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': PROG_NAME, 'version': self.version, 'copyleft': COPYLEFT})) 375 self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': PROG_NAME, 'version': self.version, 'copyleft': COPYLEFT}))
757 assert extra_outputs is None 766 assert extra_outputs is None
758 767
759 self._use_pubsub = kwargs.pop('use_pubsub', False) 768 self._use_pubsub = kwargs.pop('use_pubsub', False)
760 if self._use_pubsub: 769 if self._use_pubsub:
761 flags = kwargs.pop('pubsub_flags', []) 770 flags = kwargs.pop('pubsub_flags', [])
762 parents.add(self.host.make_pubsub_group(flags)) 771 defaults = kwargs.pop('pubsub_defaults', {})
772 parents.add(self.host.make_pubsub_group(flags, defaults))
763 self._pubsub_flags = flags 773 self._pubsub_flags = flags
764 774
765 # other common options 775 # other common options
766 use_opts = {k:v for k,v in kwargs.iteritems() if k.startswith('use_')} 776 use_opts = {k:v for k,v in kwargs.iteritems() if k.startswith('use_')}
767 for param, do_use in use_opts.iteritems(): 777 for param, do_use in use_opts.iteritems():