gflags is a simple argument parser in C++, we use it to parse the args.

When you use --help and trying to print a help message, it will burst out a lot of useless flags.

This is because of the --help is an alias of helpfull. And that is really annoying.

--help  shows all flags from all files, sorted by file and then by name; shows the flagname, its default value, and its help string
--helpfull  same as -help, but unambiguously asks for all flags (in case -help changes in the future)
--helpshort shows only flags for the file with the same name as the executable (usually the one containing main())
--helpxml   like --help, but output is in xml for easier parsing
--helpon=FILE   shows only flags defined in FILE.*
--helpmatch=S   shows only flags defined in *S*.*
--helppackage   shows flags defined in files in same directory as main()
--version   prints version info for the executable

Here is a simple way to change it:

#include "gflags/gflags.h"

DEFINE_int32(port, 8080, "your listening port"); // define for flags here

DEFINE_bool(h, false, "help"); // if you wanna to use '-h'

DECLARE_bool(help); // make sure you can access FLAGS_help
DECLARE_bool(helpshort); // make sure you can access FLAGS_helpshort

int main(int argc, char* argv[]) {
  gflags::SetUsageMessage("Usage: example-function [OPTION] ...");

  gflags::ParseCommandLineNonHelpFlags(&argc, &argv, true); // please call `NonHelpFlags` variant

  if (FLAGS_help || FLAGS_h) { // if your help or h is true, make FLAGS_help off and FLAGS_helpshort on
    FLAGS_help = false;
    FLAGS_helpshort = true;

  gflags::HandleCommandLineHelpFlags(); // add your handler back

  // your stuff here
  return 0;

The above process is making sure you always call short helper in default.

Categories: Code


Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published. Required fields are marked *