5

I'd like to pass some extra information to my init.el via command line arguments. However, the variable command-line-args-left is always nil when Emacs is run interactively. (Though it is correctly populated when run with the --script option).

How can I get a list of command line arguments when Emacs is run normally?

PythonNut
  • 10,243
  • 2
  • 29
  • 75

2 Answers2

5

The command line options are processed in startup.el.

You need to add your handlers to command-line-functions to handle the additional options.

sds
  • 5,928
  • 20
  • 39
  • Doesn't that happen before loading the init file? Will he be able to add the handlers in time? – Malabarba Jan 16 '15 at 22:39
  • @Malabarba Yeah, I'm not clear on the sequence of events. But I tried it out and adding an arg handler in my init file works fine. – glucas Jan 17 '15 at 01:50
  • Ah, looks like the args are processed in two phases. This is useful: http://www.gnu.org/software/emacs/manual/html_node/elisp/Startup-Summary.html – glucas Jan 17 '15 at 01:57
3

Arguments are deleted after being processed at startup, you can either handle them like sds specified or if you'd like the list of args given after start up, save the contents of command-line-args at the start of your init.

(setq my-save-args command-line-args)

Though if you're planning on passing custom args, you'll need to handle them during startup.

You can probably also get them from the ps command.

(shell-command-to-string (format "ps -o args -p %s" (emacs-pid)))

should return the full command emacs was run with if you have ps.

Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62
  • 1
    I see now! Why they're deleted is a mystery. I can't use a handler like @sds said, for other reasons, but it appears that `command-line-args` does exists at startup time. – PythonNut Jan 17 '15 at 00:24