Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:48:58+00:00 2026-06-15T08:48:58+00:00

I currently am tasked with creating some command-line helper utilities for our internal development

  • 0

I currently am tasked with creating some command-line helper utilities for our internal development team to use. However, I want to know the best practice for creating unix command-line tools. I have tried viewing git source code for an example of how to read parameters and display messages accordingly. However, I’m looking for a clear template for creating a tool, reading parameters safely, and displaying the standard “help” messages if a user types in an incorrect parameter or --help I want to show the help message. Is there a standard library for reading -abcFGH and --parameter and switching which process starts based upon the passed parameter?

Command-Line:

git

or

git --help

Output:

usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
       [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
       [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
       [-c name=value] [--help]
       <command> [<args>]
...

Command-Line:

MyTool CommandName --CommandArgs

Output:

Whatever that specific command does.


What I have working so far:

Code:

int main(int argc, char **argv)
{
    if(argc < 2)
    helpMessage();
    char* commandParameter = argv[1];
    if (strncmp(argv [1],"help", strlen(commandParameter)) == 0)
        helpMessage();
    else if (strncmp(argv [1],"pull", strlen(commandParameter)) == 0)
        pull();
    else
        helpMessage();
}

What would be ideal would look like this:

Code:

int main(int argc, char **argv)
{
    MagicParameters magicParameters = new MagicParameters(argv);
    switch(magicParameters[1])
    {
        case command1:
            Command1();
            break;
        case ...

        case help:
        default:
            HelpMessage();
            break;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:48:59+00:00Added an answer on June 15, 2026 at 8:48 am

    getopt_long() is what you’re looking for, here’s an example of the simplest usage:

       static const struct option opts[] = {
            {"version",   no_argument,    0, 'v'},
            {"help",      no_argument,    0, 'h'},
            {"message", required_argument, 0, 'm'},
            /* And so on */
            {0,      0,                   0,  0 }   /* Sentiel */
        };
        int optidx;
        char c;
    
        /* <option> and a ':' means it's marked as required_argument, make sure to do that.
         * or optional_argument if it's optional.
         * You can pass NULL as the last argument if it's not needed.  */
        while ((c = getopt_long(argc, argv, "vhm:", opts, &optidx)) != -1) {
            switch (c) {
                case 'v': print_version(); break;
                case 'h': help(argv[0]); break;
                case 'm': printf("%s\n", optarg); break;
                case '?': help(argv[0]); return 1;                /* getopt already thrown an error */
                default:
                    if (optopt == 'c')
                        fprintf(stderr, "Option -%c requires an argument.\n",
                            optopt);
                    else if (isprint(optopt))
                        fprintf(stderr, "Unknown option -%c.\n", optopt);
                    else
                        fprintf(stderr, "Unknown option character '\\x%x'.\n",
                            optopt);
                   return 1;
            }
        }
        /* Loop through other arguments ("leftovers").  */
        while (optind < argc) {
            /* whatever */;
            ++optind;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently on a 4-person team tasked with the development and maintenance of a
I'm currently tasked with reading some data that stored in a flat file into
I am currently tasked with porting our website code onto a linux server (from
I have been tasked with creating a reusable process for our Finance Dept to
I am in a compilers class and we are tasked with creating our own
Our team is creating a new recruitment workflow system to replace an old one.
I've been tasked with creating an IM app for IOS. I currently have an
So I've been tasked with creating a tool for our QA department that can
I'm currently trying to use PHPUnit to learn about Test Driven Development (TDD) and
I'm a web developer tasked with building a basic iOS app for internal use.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.