命令行应用

命令行是一种历史悠久且极有生命力的人机交互界面。即便在多数情况下最新的Web应用提供了各种丰富易用的人机交互体验,命令行应用依然是及其重要的工具,因为它:

  1. 适合系统管理员使用
  2. 通常只用于内部网中,所以安全性更高

ActFramework考虑了命令行应用的开发需求,特地提供了一些工具让开发命令行应用简单地不可思议。

创建命令响应器

  1. import act.app.CliContext;
  2. import act.cli.Command;
  3. import act.cli.Optional;
  4. import act.cli.Required;
  5. import act.cli.JsonView;
  6. import act.util.PropSpec;
  7. public class CustomerAdmin {
  8. @Inject
  9. private Customer.Dao customerDao;
  10. @Command(name = "cust.list", help = "list customers")
  11. @PropSpec("email,fullName as name,phone")
  12. public Iterable<Customer> list(
  13. @Optional(lead = "-q", help = "optionally specify the query string") String q
  14. ) {
  15. return customerDao.search(q);
  16. }
  17. @Command(name = "cust.show", help = "show customer details")
  18. @JsonView
  19. public Customer show(
  20. @Required(lead = "--id", help = "specify the customer ID") String id
  21. ) {
  22. return customerDao.findById(id);
  23. }
  24. }

运行命令行并执行命令

  1. #nc是一种简单的网络链接工具,如果系统中没有nc,可以使用telnet作为替代
  2. $nc localhost 5461
  3. act[1sbKUt2E1]>
  4. act[1sbKUt2E1]>help -a
  5. APPLICATION COMMANDS
  6. cust.list - list customers
  7. cust.show - show customer details
  8. act[1sbKUt2E1]>cust.list -h
  9. Usage: cust.list [options]
  10. list customers
  11. Options:
  12. -q optionally specify the query string
  13. act[1sbKUt2E1]>cust.list -q "com1.com"
  14. +--------------------------+--------------------+--------------+------------+
  15. | ID | EMAIL | NAME | PHONE |
  16. +--------------------------+--------------------+--------------+------------+
  17. | 5684c35e6e250d52baa94935 | john@com1.com | John Smith | 11,111,111 |
  18. | 569174e5b47e271add049154 | peter@com1.com | Peter Brad | 22,222,222 |
  19. +--------------------------+--------------------+--------------+------------+
  20. Items found: 2
  21. act[1sbKUt2E1]>cust.show -h
  22. Usage: cust.show [options]
  23. show customer details
  24. Options:
  25. --id specify the customer ID
  26. act[1sbKUt2E1]>cust.show --id 5684c35e6e250d52baa94935
  27. {
  28. "id": "5684c35e6e250d52baa94935",
  29. "email": "john@com1.com",
  30. "firstName": "John",
  31. "lastName": "Smith",
  32. "phone": "11,111,111",
  33. ...
  34. }

在RESTful控制器和CLI命令器上的代码复用

通常你会发现同样的逻辑总是出现在控制器和命令响应器上。这意味着你可能需要一些拷贝粘贴工作,作为一名负责任的猿,你会对此非常恼火。所幸ActFramework允许你将同样的代码同时用于控制器和命令响应器,只需使用相应的注解即可:

  1. @Controller("/customer")
  2. public class CustomerService {
  3. @Inject
  4. private Customer.Dao customerDao;
  5. @GetAction("/")
  6. @Command(name = "cust.list", help = "list customers")
  7. @PropSpec("email,fullName as name,phone")
  8. public Iterable<Customer> list(
  9. @Optional(lead = "-q", help = "optionally specify the query string") String q
  10. ) {
  11. return customerDao.search(q);
  12. }
  13. @GetAction("/{id}")
  14. @Command(name = "cust.show", help = "show customer details")
  15. @JsonView
  16. public Customer show(
  17. @Required(lead = "--id", help = "specify the customer ID") String id
  18. ) {
  19. return customerDao.findById(id);
  20. }
  21. }

回目录