Browse Source

opts, help: minor fixes & cleanups

The MMGen Project 10 months ago
parent
commit
0ec7bca054
5 changed files with 65 additions and 37 deletions
  1. 39 31
      mmgen/help/__init__.py
  2. 17 1
      mmgen/help/help_notes.py
  3. 3 2
      mmgen/main_txcreate.py
  4. 2 1
      mmgen/main_txdo.py
  5. 4 2
      mmgen/opts.py

+ 39 - 31
mmgen/help/__init__.py

@@ -44,6 +44,31 @@ def show_hash_presets(cfg):
 	msg('  N = memory usage (power of two)\n  p = iterations (rounds)')
 	msg('  N = memory usage (power of two)\n  p = iterations (rounds)')
 	sys.exit(0)
 	sys.exit(0)
 
 
+def gen_arg_tuple(cfg, func, text):
+
+	def help_notes(k):
+		import importlib
+		return getattr(importlib.import_module(
+			f'{cfg._opts.help_pkg}.help_notes').help_notes(proto, cfg), k)()
+
+	def help_mod(modname):
+		import importlib
+		return importlib.import_module(
+			f'{cfg._opts.help_pkg}.{modname}').help(proto, cfg)
+
+	from ..protocol import init_proto_from_cfg
+	proto = init_proto_from_cfg(cfg, need_amt=True)
+
+	d = {
+		'proto':      proto,
+		'help_notes': help_notes,
+		'help_mod':   help_mod,
+		'cfg':        cfg,
+	}
+
+	for arg in func.__code__.co_varnames:
+		yield d[arg] if arg in d else text
+
 def make_usage_str(cfg, caller):
 def make_usage_str(cfg, caller):
 	indent, col1_w = {
 	indent, col1_w = {
 		'help': (2, len(gc.prog_name) + 1),
 		'help': (2, len(gc.prog_name) + 1),
@@ -52,7 +77,11 @@ def make_usage_str(cfg, caller):
 	def gen():
 	def gen():
 		ulbl = 'USAGE:'
 		ulbl = 'USAGE:'
 		for line in [cfg._usage_data.strip()] if isinstance(cfg._usage_data, str) else cfg._usage_data:
 		for line in [cfg._usage_data.strip()] if isinstance(cfg._usage_data, str) else cfg._usage_data:
-			yield f'{ulbl:{col1_w}} {gc.prog_name} {line}'
+			yield '{a:{w}} {b} {c}'.format(
+				a = ulbl,
+				b = gc.prog_name,
+				c = line,
+				w = col1_w)
 			ulbl = ''
 			ulbl = ''
 	return ('\n' + (' ' * indent)).join(gen())
 	return ('\n' + (' ' * indent)).join(gen())
 
 
@@ -64,27 +93,6 @@ class Help:
 
 
 	def make(self, cfg, opts):
 	def make(self, cfg, opts):
 
 
-		def gen_arg_tuple(func, text):
-
-			def help_notes(k):
-				import importlib
-				return getattr(importlib.import_module(
-					f'{opts.help_pkg}.help_notes').help_notes(proto, cfg), k)()
-
-			def help_mod(modname):
-				import importlib
-				return importlib.import_module(
-					f'{opts.help_pkg}.{modname}').help(proto, cfg)
-
-			d = {
-				'proto':      proto,
-				'help_notes': help_notes,
-				'help_mod':   help_mod,
-				'cfg':        cfg,
-			}
-			for arg in func.__code__.co_varnames:
-				yield d[arg] if arg in d else text
-
 		def gen_output():
 		def gen_output():
 			yield '  {} {}'.format(gc.prog_name.upper() + ':', opts.opts_data['text']['desc'].strip())
 			yield '  {} {}'.format(gc.prog_name.upper() + ':', opts.opts_data['text']['desc'].strip())
 			yield make_usage_str(cfg, caller='help')
 			yield make_usage_str(cfg, caller='help')
@@ -93,21 +101,19 @@ class Help:
 			# process code for options
 			# process code for options
 			opts_text = nl.join(self.gen_text(opts))
 			opts_text = nl.join(self.gen_text(opts))
 			if 'options' in code:
 			if 'options' in code:
-				yield code['options'](*tuple(gen_arg_tuple(code['options'], opts_text)))
+				yield code['options'](*gen_arg_tuple(cfg, code['options'], opts_text))
 			else:
 			else:
 				yield opts_text
 				yield opts_text
 
 
 			# process code for notes
 			# process code for notes
 			if 'notes' in text:
 			if 'notes' in text:
 				if 'notes' in code:
 				if 'notes' in code:
-					yield from code['notes'](*tuple(gen_arg_tuple(code['notes'], text['notes']))).splitlines()
+					yield from code['notes'](*gen_arg_tuple(cfg, code['notes'], text['notes'])).splitlines()
 				else:
 				else:
 					yield from text['notes'].splitlines()
 					yield from text['notes'].splitlines()
 
 
-		from ..protocol import init_proto_from_cfg
-		proto = init_proto_from_cfg(cfg, need_amt=True)
 		text = getattr(opts, self.data_desc)['text']
 		text = getattr(opts, self.data_desc)['text']
-		code = getattr(opts, self.data_desc)['code']
+		code = getattr(opts, self.data_desc).get('code', {})
 		nl = '\n  '
 		nl = '\n  '
 
 
 		return nl.join(gen_output()) + '\n'
 		return nl.join(gen_output()) + '\n'
@@ -146,7 +152,7 @@ class GlobalHelp(Help):
 		skipping = False
 		skipping = False
 		coin_filter_codes = opts.global_filter_codes.coin
 		coin_filter_codes = opts.global_filter_codes.coin
 		cmd_filter_codes = opts.global_filter_codes.cmd
 		cmd_filter_codes = opts.global_filter_codes.cmd
-		for line in opts.global_opts_data['text']['options'][1:-3].splitlines():
+		for line in opts.global_opts_data['text']['options'][1:].rstrip().splitlines():
 			m = global_opts_help_pat.match(line)
 			m = global_opts_help_pat.match(line)
 			if m[1] == '+':
 			if m[1] == '+':
 				if not skipping:
 				if not skipping:
@@ -159,9 +165,11 @@ class GlobalHelp(Help):
 
 
 def print_help(cfg, opts):
 def print_help(cfg, opts):
 
 
-	if not 'code' in opts.opts_data:
-		opts.opts_data['code'] = {}
+	if cfg.help:
+		help_cls = CmdHelp
+	else:
+		help_cls = GlobalHelp
 
 
 	from ..ui import do_pager
 	from ..ui import do_pager
-	do_pager((CmdHelp if cfg.help else GlobalHelp)().make(cfg, opts))
+	do_pager(help_cls().make(cfg, opts))
 	sys.exit(0)
 	sys.exit(0)

+ 17 - 1
mmgen/help/help_notes.py

@@ -20,6 +20,9 @@ class help_notes:
 		self.proto = proto
 		self.proto = proto
 		self.cfg = cfg
 		self.cfg = cfg
 
 
+	def account_info_desc(self):
+		return 'account info' if self.proto.base_coin == 'ETH' else 'unspent outputs'
+
 	def fee_spec_letters(self, use_quotes=False):
 	def fee_spec_letters(self, use_quotes=False):
 		cu = self.proto.coin_amt.units
 		cu = self.proto.coin_amt.units
 		sep, conj = ((',', ' or '), ("','", "' or '"))[use_quotes]
 		sep, conj = ((',', ' or '), ("','", "' or '"))[use_quotes]
@@ -139,7 +142,20 @@ seed, the same seed length and hash preset parameters must always be used.
 		addr = t.privhex2addr('bead' * 16)
 		addr = t.privhex2addr('bead' * 16)
 		sample_addr = addr.views[addr.view_pref]
 		sample_addr = addr.views[addr.view_pref]
 
 
-		return f"""
+		if self.proto.base_coin == 'ETH':
+			return f"""
+EXAMPLES:
+
+  Send 0.123 {self.proto.coin} to an external {self.proto.name} address:
+
+    $ {gc.prog_name} {sample_addr},0.123
+
+  Send 0.123 {self.proto.coin} to another account in wallet 01ABCDEF:
+
+    $ {gc.prog_name} 01ABCDEF:{mmtype}:7,0.123
+"""
+		else:
+			return f"""
 EXAMPLES:
 EXAMPLES:
 
 
   Send 0.123 {self.proto.coin} to an external {self.proto.name} address, returning the change to a
   Send 0.123 {self.proto.coin} to an external {self.proto.name} address, returning the change to a

+ 3 - 2
mmgen/main_txcreate.py

@@ -36,7 +36,7 @@ opts_data = {
                       ‘mmgen-autosign’). The removable device is mounted and
                       ‘mmgen-autosign’). The removable device is mounted and
                       unmounted automatically
                       unmounted automatically
 -A, --fee-adjust=  f  Adjust transaction fee by factor 'f' (see below)
 -A, --fee-adjust=  f  Adjust transaction fee by factor 'f' (see below)
--B, --no-blank        Don't blank screen before displaying unspent outputs
+-B, --no-blank        Don't blank screen before displaying {a_info}
 -c, --comment-file=f  Source the transaction's comment from file 'f'
 -c, --comment-file=f  Source the transaction's comment from file 'f'
 -C, --fee-estimate-confs=c Desired number of confirmations for fee estimation
 -C, --fee-estimate-confs=c Desired number of confirmations for fee estimation
                       (default: {cfg.fee_estimate_confs})
                       (default: {cfg.fee_estimate_confs})
@@ -49,7 +49,7 @@ opts_data = {
                       See FEE SPECIFICATION below.  If omitted, fee will be
                       See FEE SPECIFICATION below.  If omitted, fee will be
                       calculated using network fee estimation.
                       calculated using network fee estimation.
 -g, --gas=         g  Specify start gas amount in Wei (ETH only)
 -g, --gas=         g  Specify start gas amount in Wei (ETH only)
--i, --info            Display unspent outputs and exit
+-i, --info            Display {a_info} and exit
 -I, --inputs=      i  Specify transaction inputs (comma-separated list of
 -I, --inputs=      i  Specify transaction inputs (comma-separated list of
                       MMGen IDs or coin addresses).  Note that ALL unspent
                       MMGen IDs or coin addresses).  Note that ALL unspent
                       outputs associated with each address will be included.
                       outputs associated with each address will be included.
@@ -69,6 +69,7 @@ opts_data = {
 	},
 	},
 	'code': {
 	'code': {
 		'options': lambda cfg, proto, help_notes, s: s.format(
 		'options': lambda cfg, proto, help_notes, s: s.format(
+			a_info = help_notes('account_info_desc'),
 			fu     = help_notes('rel_fee_desc'),
 			fu     = help_notes('rel_fee_desc'),
 			fl     = help_notes('fee_spec_letters'),
 			fl     = help_notes('fee_spec_letters'),
 			fe_all = fmt_list(cfg._autoset_opts['fee_estimate_mode'].choices, fmt='no_spc'),
 			fe_all = fmt_list(cfg._autoset_opts['fee_estimate_mode'].choices, fmt='no_spc'),

+ 2 - 1
mmgen/main_txdo.py

@@ -36,7 +36,7 @@ opts_data = {
 -A, --fee-adjust=    f Adjust transaction fee by factor 'f' (see below)
 -A, --fee-adjust=    f Adjust transaction fee by factor 'f' (see below)
 -b, --brain-params=l,p Use seed length 'l' and hash preset 'p' for
 -b, --brain-params=l,p Use seed length 'l' and hash preset 'p' for
                        brainwallet input
                        brainwallet input
--B, --no-blank         Don't blank screen before displaying unspent outputs
+-B, --no-blank         Don't blank screen before displaying {a_info}
 -c, --comment-file=  f Source the transaction's comment from file 'f'
 -c, --comment-file=  f Source the transaction's comment from file 'f'
 -C, --fee-estimate-confs=c Desired number of confirmations for fee estimation
 -C, --fee-estimate-confs=c Desired number of confirmations for fee estimation
                        (default: {cfg.fee_estimate_confs})
                        (default: {cfg.fee_estimate_confs})
@@ -107,6 +107,7 @@ FMT CODES:
 			cfg     = cfg,
 			cfg     = cfg,
 			pnm     = gc.proj_name,
 			pnm     = gc.proj_name,
 			pnl     = gc.proj_name.lower(),
 			pnl     = gc.proj_name.lower(),
+			a_info  = help_notes('account_info_desc'),
 			kgs     = help_notes('keygen_backends'),
 			kgs     = help_notes('keygen_backends'),
 			coin_id = help_notes('coin_id'),
 			coin_id = help_notes('coin_id'),
 			fu      = help_notes('rel_fee_desc'),
 			fu      = help_notes('rel_fee_desc'),

+ 4 - 2
mmgen/opts.py

@@ -150,9 +150,11 @@ def process_uopts(cfg, opts_data, opts, need_proto):
 
 
 	return uopts, uargs
 	return uopts, uargs
 
 
-cmd_opts_pat = re.compile(r'^-([a-zA-Z0-9-]), --([a-zA-Z0-9-]{2,64})(=| )(.+)')
-global_opts_pat = re.compile(r'^\t\t\t(.)(.) --([a-z0-9-]{2,64})(=| )(.+)')
+cmd_opts_pat         = re.compile(r'^-([a-zA-Z0-9-]), --([a-zA-Z0-9-]{2,64})(=| )(.+)')
+
+global_opts_pat      = re.compile(r'^\t\t\t(.)(.) --([a-z0-9-]{2,64})(=| )(.+)')
 global_opts_help_pat = re.compile(r'^\t\t\t(.)(.) (?:--([{}a-zA-Z0-9-]{2,64})(=| ))?(.+)')
 global_opts_help_pat = re.compile(r'^\t\t\t(.)(.) (?:--([{}a-zA-Z0-9-]{2,64})(=| ))?(.+)')
+
 opt_tuple = namedtuple('cmdline_option', ['name', 'has_parm'])
 opt_tuple = namedtuple('cmdline_option', ['name', 'has_parm'])
 
 
 def parse_opts(cfg, opts_data, opt_filter, global_opts_data, global_filter_codes, need_proto):
 def parse_opts(cfg, opts_data, opt_filter, global_opts_data, global_filter_codes, need_proto):