Jump to content

  • Log In with Google      Sign In   
  • Create Account

Why Does Xcode Give My Application A Command Line Argument?

  • You cannot reply to this topic
4 replies to this topic

#1 BiiXteR   Members   -  Reputation: 248

Like
0Likes
Like

Posted Today, 08:42 AM

So, today I played around a little with my main function and decided to print out the argv and argc arguments in the main function.

This resulted in the following being printed out : 

Commandline argument amount : 1.

1. /Users/name/Library/Developer/Xcode/DerivedData/Playground-bdassylsrksorocyjtssfnwfbkpg/Build/Products/Debug/Playground

Input anything to continue.
a
Program ended with exit code: 0

After the '1.' it prints out the command line argument, my question is why does it have that command line argument? And what is it used for?

 

Here's the code I used for this little program : 

#include <iostream>

int main(int argc, const char* argv[]) {
    
    std::cout << "Commandline argument amount : " << argc << "." << std::endl;
    
    for (int i = 0; i < argc; i++)
    {
        std::cout << std::endl;
        std::cout << i + 1 << ". " << argv[i] << std::endl;
    }
    
    std::cout << std::endl;
    std::cout << "Input anything to continue." << std::endl;
    std::string inpt;
    std::cin >> inpt;
    
    return 0;
}


#2 Lactose!   GDNet+   -  Reputation: 8591

Like
0Likes
Like

Posted Today, 08:52 AM

There is always at least 1 argument -- the first being the command used to invoke the program (if available, or empty if not).

A bit surprised that it isn't just the name of the application, but the entire filepath, but that might just be how Macs work?


Hello to all my stalkers.


#3 Josh Petrie   Moderators   -  Reputation: 10378

Like
0Likes
Like

Posted Today, 09:08 AM

That's how C++ works. Per the standard, if argc is non-zero argv[0] will be a pointer to the string that "represents the name used to invoke the program."

 

Xcode in particular launches your program via its absolute path.



#4 BiiXteR   Members   -  Reputation: 248

Like
0Likes
Like

Posted Today, 09:11 AM

There is always at least 1 argument -- the first being the command used to invoke the program (if available, or empty if not).

A bit surprised that it isn't just the name of the application, but the entire filepath, but that might just be how Macs work?

What would happen if the argument would be empty? :P Nothing at all? The application wouldn't launch?



#5 frob   Moderators   -  Reputation: 39056

Like
1Likes
Like

Posted Today, 09:16 AM

my question is why does it have that command line argument? And what is it used for?

 
It comes from standards that have been around since the 1970s.  Over history, many programs have used this for various reasons.
 
One reason is to change behavior based on the name of the program.  Note that decades ago storage space was extremely expensive. Many utilities were written with shared behavior, and instead of having unique programs with unique executables, a single executable with multiple symbolic links were used.  If the program were started with one name it would behave one way. If the program were started with another name it would be have another way.
 
Another reason is that it can provide additional information like the path to the program. These days there are environment variables that can also be provided to programs, or to use the concept of a program's working directory, but these did not exist originally. By passing the path to the program developers could use that to know where to look for related files, such as other executable programs.
 

What would happen if the argument would be empty? :P Nothing at all? The application wouldn't launch?



There can be zero or more arguments to programs. In C-based languages they are passed as arguments to main, traditionally called argc (argument count) and argv (argument variables).
 
Copying from one version of the standard:
 
— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
 
So yes, the count can be zero and argv[0] is undefined.  Otherwise the system is supposed to do all it can to provide the program name (whatever that means to the system) in argument argv[0].

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.






PARTNERS