在命令行中创建菜单 [英] create menus in command line

查看:204
本文介绍了在命令行中创建菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在命令行程序中创建菜单?我尝试过类似的东西:

How can one create menus in the command line program? I've tried stuff like:

cin >> input;
switch (input) {
  case (1):
    // do stuff
  case (2):
    // ...
}

但我遇到了子菜单的问题,并返回到同一菜单等。我写的第一个程序(除了练习)试图使用开关菜单的 goto 语句因为替代方案是(在当时)复杂的循环。

but then I've had the problem of sub-menus, and going back to the same menu, etc. The first program I wrote (apart from exercises) that tried to use the switch idea for the menus had goto statements because the alternative was heaps of (at the time) complicated loops.

推荐答案

如果我试图计算一个人的方式创建一个1,2,3菜单,我们都死了,之前我迭代了1/2的。但是这里有一个方法可以让你开始(未测试,你可能需要清理几个东西):

If I tried to count the ways in which one might create a 1,2,3 menu, we'd both be dead before I'd iterated 1/2 of them. But here's one method you could try to get you started (untested, you may need to clean up a couple things):

struct menu_item
{
  virtual ~menu_item() {}
  virtual std::string item_text() const = 0;
  virtual void go() = 0;
};

struct print_hello_item
{
  std::string item_text() const { return "display greeting"; }
  void go() { std::cout << "Hello there, Mr. User."; }
};

struct kill_everyone_item
{
  std::string item_text() const { return "Go on murderous rampage"; }
  void go() { for(;;) kill_the_world(); }
};

struct menu_menu_item
{
  menu_menu_item(std::string const& text) : text_(text), items() {}
  void add_item(std::unique_ptr<menu_item> item) { items.push_back(std::move(item)); }
  void go()
  {
    std::cout << "Choose: \n";
    std::for_each(items.begin(), items.end(), [](std::unique_ptr<menu_item> const& item)
    {
      std::cout << "\t" << item->item_text() << "\n";
    });
    std::cout << "\n\n\tYour choice: ";
    int choice = get_number_from_console();
    if (items.size() > choice) items[choice]->go();
  }
  std::string item_text() const { return text_; }

private:
  std::string text_;
  std::vector<std::unique_ptr<menu_item> > items;
};

int main()
{
  menu_menu_item top_item;
  top_item.add(std::unique_ptr<menu_item>(new print_hello_item));
  top_item.add(std::unique_ptr<menu_item>(new kill_everyone_item));

  top_item.go();
}

作为练习,我如何定义菜单项如下:

As an exercize, how might I define menu items like so:

top_level.add()
  ( "Drive off a cliff", &die_function )
  ( "Destroy the world", &global_thermal_nuclear_war )
  ( "Deeper", submenu()
                ( "Hey, check this shit out!", &gawk ))
;

可以使用上述框架作为起点。

It can be done with the above framework as a starting point.

这是OO设计和可能被称为程序性之间的区别。我创建了一个抽象背后的意思是作为一个菜单选择(它可以是另一个菜单),可以在各个方向扩展。我创建了我需要的扩展,把它们放在一起,告诉事情去。好的OO设计就是这样...你的程序的主要部分是把东西放在一起,告诉它去。

This is the difference between OO design and what might be called "procedural". I created an abstraction behind what it means to be a menu choice (which can be another menu) that can be extended in various directions. I create the extensions I need, put them together, and tell the thing to go. Good OO design is just like that...the main part of your program is comprised of putting stuff together and telling it to go.

从这个关键的东西不一定要按照我刚才所做的方式,而是以不同的方式来思考它。如果你能得到上述代码的要点,你会看到,你可以添加新的项目,新的菜单,任意深度,而不必处理那种过于复杂的代码,切换风格导致。

The key thing to take from this is not necessarily to do it the way I just did, but to think about it in a different way. If you can get the gist of the above code then you'll see that you can add new items, with new menus, to arbitrary depths, without having to deal with the kind of overly complicated code that the switch style causes.

这篇关于在命令行中创建菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆