TclCommandHelp.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Content was borrowed from FlatCAM proper #
  5. # Date: 4/22/2020 #
  6. # MIT Licence #
  7. # ##########################################################
  8. from tclCommands.TclCommand import TclCommand
  9. import collections
  10. import math
  11. import gettext
  12. import FlatCAMTranslation as fcTranslate
  13. import builtins
  14. fcTranslate.apply_language('strings')
  15. if '_' not in builtins.__dict__:
  16. _ = gettext.gettext
  17. class TclCommandHelp(TclCommand):
  18. """
  19. Tcl shell command to show Help
  20. example:
  21. help add_circle
  22. """
  23. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  24. aliases = ['help']
  25. description = '%s %s' % ("--", "PRINTS to TCL the HELP.")
  26. # Dictionary of types from Tcl command, needs to be ordered
  27. arg_names = collections.OrderedDict([
  28. ('name', str)
  29. ])
  30. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  31. option_types = collections.OrderedDict([
  32. ])
  33. # array of mandatory options for current Tcl command: required = {'name','outname'}
  34. required = []
  35. # structured help for current command, args needs to be ordered
  36. help = {
  37. 'main': "Returns to TCL the value for the entered system variable.",
  38. 'args': collections.OrderedDict([
  39. ('name', 'Name of a Tcl Command for which to display the Help.'),
  40. ]),
  41. 'examples': ['help add_circle']
  42. }
  43. def execute(self, args, unnamed_args):
  44. """
  45. :param args: Without any argument will display the list of commands. Can have as a argument
  46. a tcl command name to display the help for that command.
  47. :param unnamed_args:
  48. :return:
  49. """
  50. if 'name' in args:
  51. name = args['name']
  52. if name not in self.app.tcl_commands_storage:
  53. return "Unknown command: %s" % name
  54. self.app.shell.append_output(self.app.tcl_commands_storage[name]["help"])
  55. else:
  56. if not args:
  57. cmd_enum = '%s\n' % _("Available commands:")
  58. displayed_text = []
  59. try:
  60. # find the maximum length of a command name
  61. max_len = 0
  62. for cmd_name in self.app.tcl_commands_storage:
  63. curr_len = len(cmd_name)
  64. if curr_len > max_len:
  65. max_len = curr_len
  66. h_space = " "
  67. cnt = 0
  68. for cmd_name in sorted(self.app.tcl_commands_storage):
  69. cmd_description = "<span>%s</span>" % self.app.tcl_commands_storage[cmd_name]['description']
  70. curr_len = len(cmd_name)
  71. cmd_name_colored = "<span style=\" font-weight: bold; color: red;\" >%s</span>" % str(cmd_name)
  72. nr_chars = max_len - curr_len
  73. cmd_line_txt = '%s%s&nbsp;&nbsp;%s' % (cmd_name_colored, nr_chars * h_space, cmd_description)
  74. displayed_text.append(cmd_line_txt)
  75. # group commands by 4 adding a line break after 4 commands
  76. if cnt == 3:
  77. cnt = 0
  78. displayed_text.append(' ')
  79. else:
  80. cnt += 1
  81. except Exception as err:
  82. self.app.log.debug("App.setup_shell.shelp() when run as 'help' --> %s" % str(err))
  83. displayed_text = ['> %s\n' % cmd for cmd in sorted(self.app.tcl_commands_storage)]
  84. cmd_enum += '<br>'.join(displayed_text)
  85. cmd_enum += '<br><br>%s<br>%s' % (_("Type help <command_name> for usage."), _("Example: help open_gerber"))
  86. self.app.shell.append_raw(cmd_enum)