1

I'm pretty new to looking at D (like...yesterday, after looking for Kotlin benchmarks...) and currently trying to decide if it's a language I want to cope with.

I'm trying to pass some arguments from command line and I'm a little surprised. Let's say I pass "-Foo -Bar". My program is quite simple:

import std.stdio;

void main(string [] args) {

    foreach(arg; args) {
        writeln(arg);
    }
}

Coming from Java, I expected it to print

  • -Foo
  • -Bar

But my D program seems to receive its location as the first argument? The output is:

  • /home/(username)/Java_Projects/HelloD/hellod
  • -Foo
  • -Bar

I tried searching for this, but all Google hits refer to Java's -D switch... So, is this intended behaviour? If yes, does anyone know why?

Max Alibaev
  • 681
  • 7
  • 17
Silverclaw
  • 1,316
  • 2
  • 15
  • 28
  • 1
    Yes, C++ (and many other langages) works this way. The first args is always to path to the executable. – Nil Jul 13 '16 at 17:54
  • Possible duplicate of [Is args\[0\] guaranteed to be the path of execution?](http://stackoverflow.com/questions/383973/is-args0-guaranteed-to-be-the-path-of-execution) – Patrick Bell Jul 13 '16 at 20:59
  • That "duplicate" is for a different language. – Silverclaw Jul 13 '16 at 21:20

1 Answers1

7

That's normal in D, inherited from C and C++. The first argument is the name of the program so you can use it to determine which function you want in a multi-use program.

The busybox unix toolset https://busybox.net/ uses this (well, at least used to, I'm not sure if it has changed) so one program, busybox, can be called as various unix commands like ls or cp.

Using args[0], it can tell which one it was called as, though they all point to the same binary program, and respond accordingly.


TIP: if you're not interested in this, you can loop just your args with foreach(arg; args[1 .. $]) {}

Adam D. Ruppe
  • 25,382
  • 4
  • 41
  • 60
  • Thanks. I had avoided C-like languages so far and did not now it was normal there. And it's pretty hard to google for anything D-related. x__x – Silverclaw Jul 13 '16 at 18:03
  • I can confirm busybox still uses this, another example is mkfs where fs-specific commands like mkfs.ntfs are symlinks to the only binary mkfs. – cym13 Jul 13 '16 at 20:30
  • For a bit easier google-ability use "dlang" or "d programming language" instead of just D. – Meta Jul 13 '16 at 20:44
  • I did, but that only helps to some very limited extent. – Silverclaw Jul 13 '16 at 21:19
  • It's not only about C-like languages, Python has program's name in sys.argv[0], too. But since Python is written in C, you may call this «inherited from C» as well. :-) – Max Alibaev Jul 14 '16 at 08:41
  • 1
    Btw, maybe it's better to warn you that args[0] won't neccessary be full path to the binary. AFAIK, it may be relative path or path to a symlink, depending on how exactly the program was started. – Max Alibaev Jul 14 '16 at 08:43
  • Indeed, it rarely is actually the full path, it is the first part of the *command* the user entered, which is sometimes the path but not necessarily. Programs calling other programs can put in arbitrary data there too (and btw it is a mutable string at the lowest level, D copies it into an immutable, but if you use the lower level functions, a program can also modify that string - some use this so the `ps` command shows something different, like if you fork and want the daemon to stand out in `ps`. In D, `import core.runtime; Runtime.cArgs.argv` is the mutable version. – Adam D. Ruppe Jul 14 '16 at 12:09
  • 1
    In fact, it's not a C thing but a Unix thing defined by the `exec` system call. Programming languages do only reflect that what the OS provides. Java being somehow its own OS, it could redefine its machine model as it wanted (probably to increase portability). So the odd man here is Java, not D. – Patrick Schlüter Jul 15 '16 at 07:15
  • @Adam, the mutability of the `args` is also OS specific. For instance on Solaris you can change the content of args, but the change is only visible to the process that changed the value. Os tools like `ps`, `prstat` or `top` will not be influenced. The mutation is not reflected in the `/proc` file system (contrary to Linux). – Patrick Schlüter Jul 15 '16 at 07:21