main_tool.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 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 mmgen.common import *
  23. def make_cmd_help():
  24. import mmgen.tool
  25. out = []
  26. for bc in mmgen.tool.MMGenToolCmd.__bases__:
  27. cls_doc = bc.__doc__.strip().split('\n')
  28. for l in cls_doc:
  29. if l is cls_doc[0]: l += ':'
  30. l = l.replace('\t','',1)
  31. if l:
  32. l = l.replace('\t',' ')
  33. out.append(l[0].upper() + l[1:])
  34. else:
  35. out.append('')
  36. out.append('')
  37. cls_funcs = bc._user_commands()
  38. max_w = max(map(len,cls_funcs))
  39. fs = ' {{:{}}} - {{}}'.format(max_w)
  40. for func in cls_funcs:
  41. m = getattr(bc,func)
  42. if m.__doc__:
  43. out.append(fs.format(func,
  44. pretty_format( m.__doc__.strip().replace('\n\t\t',' '),
  45. width=79-(max_w+7),
  46. pfx=' '*(max_w+5)).lstrip()
  47. ))
  48. out.append('')
  49. return '\n'.join(out)
  50. opts_data = {
  51. 'text': {
  52. 'desc': 'Perform various {pnm}- and cryptocoin-related operations'.format(pnm=g.proj_name),
  53. 'usage': '[opts] <command> <command args>',
  54. 'options': """
  55. -d, --outdir= d Specify an alternate directory 'd' for output
  56. -h, --help Print this help message
  57. --, --longhelp Print help message for long options (common options)
  58. -k, --use-internal-keccak-module Force use of the internal keccak module
  59. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  60. for password hashing (default: '{g.hash_preset}')
  61. -P, --passwd-file= f Get passphrase from file 'f'.
  62. -q, --quiet Produce quieter output
  63. -r, --usr-randchars=n Get 'n' characters of additional randomness from
  64. user (min={g.min_urandchars}, max={g.max_urandchars})
  65. -t, --type=t Specify address type (valid options: 'legacy',
  66. 'compressed', 'segwit', 'bech32', 'zcash_z')
  67. -v, --verbose Produce more verbose output
  68. """,
  69. 'notes': """
  70. COMMANDS
  71. {ch}
  72. Type '{pn} help <command>' for help on a particular command
  73. """
  74. },
  75. 'code': {
  76. 'options': lambda s: s.format(g=g),
  77. 'notes': lambda s: s.format(
  78. ch=make_cmd_help(),
  79. pn=g.prog_name)
  80. }
  81. }
  82. cmd_args = opts.init(opts_data,add_opts=['hidden_incog_input_params','in_fmt','use_old_ed25519'])
  83. if len(cmd_args) < 1: opts.usage()
  84. cmd = cmd_args.pop(0)
  85. import mmgen.tool as tool
  86. tc = tool.MMGenToolCmd()
  87. if cmd in ('help','usage') and cmd_args:
  88. cmd_args[0] = 'command_name=' + cmd_args[0]
  89. if cmd not in dir(tc):
  90. die(1,"'{}': no such command".format(cmd))
  91. args,kwargs = tool._process_args(cmd,cmd_args)
  92. ret = getattr(tc,cmd)(*args,**kwargs)
  93. tool._process_result(ret,pager='pager' in kwargs and kwargs['pager'],print_result=True)