Help with command line arguments?

Help with command line arguments?

Post by the0nlyb0ss on Sun Oct 03, 2010 8:37 pm
([msg=46988]see Help with command line arguments?[/msg])

I tried writing a replacement for "dir" because I'm so accustomed to using "ls". I wrote it successfully in Python, but wanted it to be faster so I decided to write it C++, which I have very limited knowledge in. Here's my code:
Code: Select all
#include <iostream>
#include <string>

using namespace std;

void usage();
void version();

void version()
{
   cout << endl;
   cout << "VERSION : 0.1 BETA\n";
   cout << "AUTHOR  : the0nlyb0ss\n";
   cout << "DATE    : 10/2/2010\n";
   cout << "OS      : Windows\n";
   cout << endl;
   cout << "LS.EXE was created as a 'dir' subsitute for all us UNIX lovers out there.\n";
   cout << "It takes a few of the common argv for 'ls' such as '-l' and '-a'.\n";
   cout << endl;
}   

void usage()
{
   cout << endl;
   cout << "USAGE: ls <options> <folder>" << endl << endl;
   cout << "\t-h     : Show this help text" << endl;
   cout << "\t-v     : Show version and program info" << endl;
   cout << "\t-l     : Show files with owner information" << endl;
   cout << "\t-a     : Show only hidden files" << endl;
}

int main(int argc, char* argv[])
{
   if(argc < 1 || argc > 3){
      usage();
   }
   if(argc == 1){
      system("DIR");
   }
      if(argv[1] == "-h"){
         usage();
      }
      else if(argv[1] == "-v"){
         version();
      }
      else if(argv[1] == "-l"){
         system("DIR /Q");
      }
      else if(argv[1] == "-a"){
         system("DIR /A:H");
      }
      else if(argv[1] == "-la"){
         system("DIR /A:H /Q");
      }
   return 0;
}


The program works fine when I simply type "ls", and it also gives me the usage() function when I type more than 3 arguments, but when I try typing any valid arguments, whether it's "-h", "-a", "-l", "-v", or "-la", it doesn't do anything at all.
Last edited by the0nlyb0ss on Sun Oct 03, 2010 9:14 pm, edited 1 time in total.
"Knowledge is knowing that a tomato is a fruit, but Wisdom is knowing not to put it in a fruit salad."
User avatar
the0nlyb0ss
Experienced User
Experienced User
 
Posts: 54
Joined: Thu Sep 02, 2010 11:24 pm
Location: California
Blog: View Blog (0)


Re: Help with command line arguments?

Post by Goatboy on Sun Oct 03, 2010 8:53 pm
([msg=46992]see Re: Help with command line arguments?[/msg])

EDIT: I see you did that already.

Does it work for -v, -a, and -la?
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2806
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Help with command line arguments?

Post by the0nlyb0ss on Sun Oct 03, 2010 9:14 pm
([msg=46995]see Re: Help with command line arguments?[/msg])

No, no arguments work at all. No matter what I put whether it be "-h", "-v", "-l", "-a", or "-la" :cry:
I'll edit the OP to say that to avoid confusion
"Knowledge is knowing that a tomato is a fruit, but Wisdom is knowing not to put it in a fruit salad."
User avatar
the0nlyb0ss
Experienced User
Experienced User
 
Posts: 54
Joined: Thu Sep 02, 2010 11:24 pm
Location: California
Blog: View Blog (0)


Re: Help with command line arguments?

Post by Assassian360 on Sun Oct 03, 2010 11:11 pm
([msg=46999]see Re: Help with command line arguments?[/msg])

The problem is what you are doing with the command line arguments.

For example:
if(argv[1] == "-h")

You can't use the == here like this. You won't actually be comparing the contents of each of these. Instead pointers will be compared. This will mean none of your checks will ever be true. So instead I suggest you lookup comparing strings in C++. Or otherwise look for existing solutions for getting command line options just like what you are doing. Sadly the best version of this type of thing that I have seen is a Linux code file, but there should be windows variations around.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Help with command line arguments?

Post by thetan on Mon Oct 04, 2010 11:15 am
([msg=47015]see Re: Help with command line arguments?[/msg])

The traditional and accepted _*nix_ way to do this is to use getopt()
http://linux.die.net/man/3/getopt

If you're trying to write portable code in C++ i suggest you take a look at the boost library.
http://www.boost.org/doc/libs/1_44_0/do ... tions.html
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Help with command line arguments?

Post by the0nlyb0ss on Thu Oct 07, 2010 10:08 pm
([msg=47211]see Re: Help with command line arguments?[/msg])

Thank you guys! This just goes to show how little (none?) I know about C++, i decided to just make it do the standard "dir" without any options :P
Now I'm going to start seriously learning C++ and maybe somewhere along the road I'll look back on this and say "Doh!" :)
"Knowledge is knowing that a tomato is a fruit, but Wisdom is knowing not to put it in a fruit salad."
User avatar
the0nlyb0ss
Experienced User
Experienced User
 
Posts: 54
Joined: Thu Sep 02, 2010 11:24 pm
Location: California
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests