main_tool.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen-tool: Perform various MMGen- and cryptocoin-related operations.
  20. Part of the MMGen suite
  21. """
  22. from .common import *
  23. def make_cmd_help():
  24. import mmgen.tool
  25. def do():
  26. for bc in mmgen.tool.MMGenToolCmds.classes.values():
  27. cls_doc = bc.__doc__.strip().split('\n')
  28. for l in cls_doc:
  29. if l is cls_doc[0]:
  30. l += ':'
  31. l = l.replace('\t','',1)
  32. if l:
  33. l = l.replace('\t',' ')
  34. yield l[0].upper() + l[1:]
  35. else:
  36. yield ''
  37. yield ''
  38. max_w = max(map(len,bc.user_commands))
  39. for name,code in sorted(bc.user_commands.items()):
  40. if code.__doc__:
  41. yield ' {:{}} - {}'.format(
  42. name,
  43. max_w,
  44. pretty_format(
  45. code.__doc__.strip().replace('\n\t\t',' '),
  46. width = 79-(max_w+7),
  47. pfx = ' '*(max_w+5)).lstrip()
  48. )
  49. yield ''
  50. return '\n'.join(do())
  51. opts_data = {
  52. 'text': {
  53. 'desc': f'Perform various {g.proj_name}- and cryptocoin-related operations',
  54. 'usage': '[opts] <command> <command args>',
  55. 'options': """
  56. -d, --outdir= d Specify an alternate directory 'd' for output
  57. -h, --help Print this help message
  58. --, --longhelp Print help message for long options (common options)
  59. -k, --use-internal-keccak-module Force use of the internal keccak module
  60. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  61. for password hashing (default: '{g.dfl_hash_preset}')
  62. -P, --passwd-file= f Get passphrase from file 'f'.
  63. -q, --quiet Produce quieter output
  64. -r, --usr-randchars=n Get 'n' characters of additional randomness from
  65. user (min={g.min_urandchars}, max={g.max_urandchars})
  66. -t, --type=t Specify address type (valid options: 'legacy',
  67. 'compressed', 'segwit', 'bech32', 'zcash_z')
  68. -v, --verbose Produce more verbose output
  69. -X, --cached-balances Use cached balances (Ethereum only)
  70. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  71. """,
  72. 'notes': """
  73. COMMANDS
  74. {ch}
  75. Type '{pn} help <command>' for help on a particular command
  76. """
  77. },
  78. 'code': {
  79. 'options': lambda s: s.format(g=g),
  80. 'notes': lambda s: s.format(
  81. ch=make_cmd_help(),
  82. pn=g.prog_name)
  83. }
  84. }
  85. cmd_args = opts.init(opts_data,add_opts=['hidden_incog_input_params','in_fmt'])
  86. if len(cmd_args) < 1:
  87. opts.usage()
  88. cmd = cmd_args.pop(0)
  89. import mmgen.tool as tool
  90. if cmd in ('help','usage') and cmd_args:
  91. cmd_args[0] = 'command_name=' + cmd_args[0]
  92. if cmd not in tool.MMGenToolCmds:
  93. die(1,f'{cmd!r}: no such command')
  94. args,kwargs = tool._process_args(cmd,cmd_args)
  95. ret = tool.MMGenToolCmds.call(cmd,*args,**kwargs)
  96. if type(ret).__name__ == 'coroutine':
  97. ret = run_session(ret)
  98. tool._process_result(ret,pager='pager' in kwargs and kwargs['pager'],print_result=True)