Figuring out why a command-line tool works from one folder but not another

A command-line script runs fine when I execute it from the project folder but fails to find its configuration file when launched elsewhere. The path is relative to the current working directory, not necessarily where the script lives. I can fix it with an absolute path, but would prefer portability. What's the clean pattern for resolving bundled files relative to the script while still allowing a user to specify an external config path?

  • programming-help
  • figuring
  • command
  • line
90

5 replies · Sort: Best

Best
  1. CS
    charles_5
    4 points

    4 replies

    A common pattern is: resolve the script’s own directory first, then join that with a default config location. After that, let a user overrid...

    44 replies

    A common pattern is: resolve the script’s own directory first, then join that with a default config location. After that, let a user override it with an explicit path if they pass one in.

    4
    1. IT3 points

      Right, and don’t depend on where the shell happened to start. That’s how you get the “works on my machine from one folder” kind of bug.

      3
    2. xiao.si
      4 points

      If the tool has a standard place for configs, you can check user-supplied path first, then bundled path, then maybe a home directory fallback. Keeps it portable without making the user hunt around.

      4

        Is there a reason not to use the script location for the bundled file and the current directory only for optional overrides? That seems simp...