Advertisement
zamotivator

command-line parser DSL

Aug 6th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. root := help:
  2. root.print_help()
  3. exit(-2)
  4. help := '-h'
  5. help += '--help'
  6.  
  7. root += version:
  8. root.print_version()
  9. exit(-1)
  10. version := '-v'
  11. version += '--version'
  12.  
  13. root += command
  14. root += 'help' command:
  15. command.print_help()
  16. root += command '--help' => 'help' command
  17.  
  18. root += 'version' command:
  19. command.print_version()
  20. root += command '--version' => 'version' command:
  21.  
  22. command := 'node' node
  23. command.node := 'list':
  24. node_list()
  25.  
  26. command.node += 'attach' name connect_string optional(force):
  27. if node.is_available(node_name):
  28. message = 'node "%s" is already defined' % name
  29. if force:
  30. root.warning('%s, would be replaced' % message)
  31. else:
  32. root.error(message)
  33. node_attach(name, connect_string)
  34.  
  35. command.node.connect_string := user host port:
  36. return dict(user=user, host=host, port=port)
  37.  
  38. command.node.connect_string.user := '([a-z]+[a-z0-9._]*)@':
  39. return args[1]
  40.  
  41. host := '[a-z]+[a-z0-9._]*'
  42. host += '(digit{1,3})\.(digit{1,3})\.(digit{1,3})\.(digit{1,3})':
  43. result = []
  44. for byte in args[1:]:
  45. if int(byte) > 255:
  46. root.error('invalid ip address')
  47. else:
  48. result.append(int(byte))
  49. return args[0]
  50. command.node.connect_string.port := '(digit)+:' :
  51. if int(args) > 2**16:
  52. root.error('too big port number')
  53. else:
  54. return int(args)
  55.  
  56. command.node.detach := 'detach' name optional(force):
  57. if not node.is_available(node_name):
  58. message = 'node "%s" did not found' % name
  59. if force:
  60. root.warning(message)
  61. node_detach(name)
  62. else:
  63. root.error(message)
  64. else:
  65. node_detach(name)
  66.  
  67. name := simple_name
  68. name += complex_name
  69. simple_name := '[a-zA-Z]+[a-zA-Z0-9]*'
  70. complex_name := '"([a-zA-Z]+[a-zA-Z0-9 ])*"':
  71. return args[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement