Thread: How to label command line arguments?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    How to label command line arguments?

    Hi, I'm not sure if this is the right forum for this, but how do I label command line arguments? It might be called something different...but what I mean is like when I run a program like
    ./someprogram --arg1=yes --arg2=no or ./someprogram --help all

    How do I set that up in code? I tried googling the issue, but couldn't really find anything. If someone just has a link I can go read that would be great.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by SterlingM View Post
    Hi, I'm not sure if this is the right forum for this, but how do I label command line arguments? It might be called something different...but what I mean is like when I run a program like
    ./someprogram --arg1=yes --arg2=no or ./someprogram --help all

    How do I set that up in code? I tried googling the issue, but couldn't really find anything. If someone just has a link I can go read that would be great.
    Take a look at: How to access command line arguments-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    I know how to USE command line arguments...but how do you make it so they are inputted with labels?

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    You type two dashes, an argument name, an equals sign and then what you want the value to equal. And then in your program, you use the normal C/C++ string functions to parse them. It's exactly the same process you use for non labelled arguments (using your nomenclature).

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Oh so there is nothing special in the code to be done? You just check for the labels? And is there any difference between two dashes and one or is that just a preference?

  6. #6
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    It's only a matter of preference. You can make it whatever you want it. I think it has a meaning and there must be a convention around it but for all you care you could make it all single dash or all double-dash and it would change nothing. It's just text, you decide what it does.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SterlingM View Post
    Hi, I'm not sure if this is the right forum for this, but how do I label command line arguments? It might be called something different...but what I mean is like when I run a program like
    ./someprogram --arg1=yes --arg2=no or ./someprogram --help all

    How do I set that up in code? I tried googling the issue, but couldn't really find anything. If someone just has a link I can go read that would be great.
    If you're talking about a set of flags that let you identify a parameter from a word or a path... Most people use dashes on Linux and slashes on Windows. Although you will find examples of both on both OSs.

    For easy parsing, make a defined structure for your flags ... --FLAG:VALUE... /FLAG=VALUE... Inside the program they're just strings so you get to sort them out manually.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by SterlingM View Post
    Oh so there is nothing special in the code to be done? You just check for the labels? And is there any difference between two dashes and one or is that just a preference?
    My experience has showed me that the more common standard is to use two dashes when whole words are used for parameter names, and singe dashes when a single letter is used. Many unix/linux programs do it this way, although it should be noted that GCC does not.

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    If your target OS is POSIX.2 or you're using GCC, you can use the getopt library.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could also consider Boost.Program_options, especially if Boost has already been installed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By stefanyco in forum C++ Programming
    Replies: 25
    Last Post: 08-04-2011, 03:58 AM
  2. Command Line Arguments
    By DuckCowMooQuack in forum C Programming
    Replies: 5
    Last Post: 03-30-2011, 03:33 PM
  3. Command Line Arguments
    By Lang in forum C Programming
    Replies: 8
    Last Post: 03-29-2008, 02:54 AM
  4. arguments from a command line
    By smegly in forum C Programming
    Replies: 3
    Last Post: 05-24-2004, 09:28 AM
  5. command line arguments
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-20-2001, 11:07 AM