开发者

is there some c++ library / source similar to boost program_options but for *keyboard shortcut auto-gen help* ?

So, I have some c++ source with key bindings like:

  switch( keypressed )
{
case 'c':
  cam_handle->Yaw(min_angle );
  break;
case 'd':
  cam_handle->Yaw( -min_angle );
  break;
case 's':
  cam_handle->Pitch(min_angle );
  break;
case 'x':
  cam_handle->Pitch( -min_angle );
  break;
case 'z':
  cam_handle->Roll( min_angle );
  break;
case 'a':
  cam_handle->Roll( -min_angle );

I always forget what the stupid keys are and have to guess, and they might change, or new keys get added, etc. Is there some fast way to auto-generate help or an "idiot's guide" popup that that says what the short-cuts are? In case someone doesn't know boost::program_options but can answer, here's an example of that:

int options(int ac, char ** av, Options& opts) {
// Declare the supported options.
po::options_description desc("Allowed options");
desc.add_options()
  ("help", "Produce help message.")
  ("width,w", po::value<int>(&opts.frameWidth)->default_value(640),"frame width")
  ("height,h", po::value<int>(&opts.frameHeight)->default_value(480),"frame height")
  ("port,p", po::value<string>(&opts.port)->default_value("5001"),"port");

 po::variables_map vm;
 po::store(po::command_line开发者_高级运维_parser(ac, av).options(desc).allow_unregistered().run(),vm);
 po::notify(vm);

 if (vm.count("help")) {
  cout << desc << "\n";
  return 1;
 }

 return 0;
}

So, I don't have to "RTFC" to know how to use the executable, I just say "./myapp --help" and boost has nicely auto-generated help and all that. Is there something like this for keyboard shortcut mapping, where the key strokes replace the role of commandline flags? (In C++ that is. In principle C is OK too but I doubt it could be as elegant as the boost stuff. )


In my opinion these are two different pairs of shoes. If you like to create and ship documentation about your application I think it might not be the best to make it accessible via a --help option. It might be okay for small programs, but boost::program_options is certainly not the best way to deal with this.

If you want to make a --help switch, you have to look up the argc and argv arguments for your main function:

int main(int argc, char** argv)
{
    for(int i = 0; i < argc; ++i)
    {
        if(std::string(argv[i]) == "--help")
        {
            // TODO print usage keys and quit
        }
    }
    ...
}

I think, the ideal way would be to make the keys configurable and to save the actions with the according keys into a configuration file which gets interpreted in your application. So the user can always see the actions available for your application and can also configure them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜