Hello,

I'm looking for some advice (NOT CODING) on a project I'm hoping to start..

Basically, I'm hoping to develop some shell commands that work a tiny bit like Github (if you are familular with it)..
The language that I want to use is C++..

Could anyone offer any advice please? Anywhere I can start reading up on how to do such things..

Thanks :)!

Recommended Answers

All 7 Replies

I'm not familiar with Github. Can you give an example?

What OS are you using? It makes a difference.

Also, if you're looking to do a git like command, are you familiar with socket programming?

Next, if you're connecting to a remote server (the way git does) how is the server going to handle your request and in what language?

Lastly, if so, do you have a server you can use?

Git is built like most other Unix/Linux tools, as a set of command-line tools (programs) with options. The best library for the purpose of creating an option-rich command-line program is the Boost.Program-Options.

If what you want to do is create a shell program (like bash), then it's a whole other ball-park. Probably not worth the effort just for one project. I believe that the windows version of Git, called "Git Bash" is simply the Unix/Linux Git tool wrapped inside a cygwin bash application. So, this is probably the route you should take too.

I suggest you develop your application as a Unix/Linux tool (which relies on a powerful shell). Either use cygwin on Windows, or use a Linux distro installation of some kind.

Setting the overall goal aside for the time being, what it sounds like is that you want help in processing command-line arguments. While the details of this can be rather tricky, the basics are fairly straightforward.

In C and C++, command-line arguments are passed to the program as a pair of optional parameters for the main() function. These parameters are an int value and an array of array of char. By convention, these are named argc (for 'argument count') and argv (for 'argument values'). The full function header of main() thus becomes:

int main(int argc, char* argv[])

Generally, you would start by checking the value of argc to see if any arguments were passed, and if so, how many. You would then check the values in the individual sub-arrays of argvto get the actual arguments. By convention, under most systems, the zeroth argument is the path of the executable program itself.

To give a simple example, here is a simple program that echos the arguments to stdout:

#include <iostream>
#include <cstdlib>

int main(int argc, char* argv[])
{
    if (argc < 1)
    {
        std::cerr << argv[0] << ": no arguments passed" << endl;
        exit(-1);
    }
    else 
    {
        for (int i = 1; i <= argc; i++)
        {
            std::cout << argv[i];    
        }
    }    
    return 0;
}

Now, this doesn't really process the arguments much; it accepts no flags or switches, and doesn't actually use the values of the arguments other than to repeat them back out. For a more extensive example (in C, but the principles are the same), take a look at my PMac-Passim program source, which does some trivial option handling.

For more extensive parameter processing, you might consider using some sort of argument option tool, such as the GNU getopt function, or the similar POSIX format getopts. A more extensive, but Linux-specific alternative is popt.

Just a quick correction: line 6 should read

if (argc == 1)

and line 13 should read

for (int i = 1; i < argc; i++)

HTH.

commented: I was withholding rep because of line 13, but now you deserve it. :) +8

Just a quick correction: line 6 should read

if (argc == 1)

It's a nitpick, but I wouldn't recommend that because argc can indeed be 0. If you're expecting one actual argument then I'd make the test argc < 2. This accounts for all cases where insufficient arguments are provided. Whether the program name (stored in argv[0]) is available or not, actual parameters will begin at argv[1]. So there's no case where argv[0] will represent anything except for a null pointer or the program name.

So the test argc < 2 covers all of the possible cases:

  • argc is 0 (program name is not available, no parameters)
  • argc is 1 (program name is available or simulated, no parameters)
  • argc is 2 or more (parameters provided, program name available or simulated)

On a side note, it's not safe to assume that argv[0] represents anything meaningful. It could be NULL in the case where argc is 0, or it could be an empty string. Even if it's not an empty string, the value is a completely implementation-defined representation of the program name.

Thank you for pointing both of those issues out. I should have been more careful in this, but I was a bit rushed. My apologies if I misled anyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.