1

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. )

peter karasev
  • 2,578
  • 1
  • 28
  • 38
  • If you have any required options, you should run your [help option code](http://stackoverflow.com/questions/5395503/required-and-optional-arguments-using-boost-library-program-options/5517755#5517755) prior to `po::notify(vm)` as it processes the errors, like a missing option. Since you don't have them in your example, then your code is fine. But, it is something to watch for. – rcollyer Oct 05 '11 at 21:41

1 Answers1

1

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.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • plus 0.5, rounded up :-) Sounds like "no" there's not an easy way. Config file makes sense I guess, I just thought maybe there is a auto-generated popup window maker out there to show the key bindings. Config file is not much better than having it hardcoded, as the c++ is for dissemination by people who can read c++ and integrate it somewhere, just not write the app from scratch. – peter karasev Oct 02 '11 at 00:32
  • Arr this came back to bite me big-time .... started collaborating with a guy using German keyboard layout, a config file or something is a must, because now we're clashing about whether "z,x" or "y,x" are +/- toggles, and "r,t,y" vs. "r,t,z" as 3-axis rotation keys- everyone wants the logically adjacent keys! – peter karasev Nov 15 '11 at 19:55