I write a lot of software tools to simplify tasks. Some of these follow a workflow like this:
- In the terminal I type the command to invoke the tool
- The tool reads some configuration file from disk either from the current workdir or from some provided path.
- The tool operates using the config it found. Part of this config could be paths to other files.
Every time I implement something like this I am wondering about the best answer to the following question: How should the tools interpret relative paths in the settings? The reason is that there are two quite obvious choices for this:
(1) Relative to the current workdir where the terminal is operating from or
(2) relative to the file that contained the settings.
From a developer's standpoint both of these seem to be reasonable choices at first and are easy to implement. However, implementing both of these can become increasingly complicated since, to allow for maximum "freedom" for the user, it would be necessary to allow each path stored to be either interpreted as in (1) or in (2).
What would be the user-friendliest way of implementing this? By "user" I am referring to anyone that might want to use the tool I wrote that is not a developer themself.
Edit
Maybe I should have added some detail. This settings file is not a "global config" for the tool, but rather something local. For example, it could tell the tool how to construct some files in some directory.
In other words, the user/terminal probably was in this directory anyways. The settings file is in some directory and from there, the tool operates. Let's look at an example. Instead of an actual tool, I will take python for this.
The "tool":
import os
from os import path
import sys
cwd = os.getcwd()
# Argument passed is the path to the settings file;
# since it is from the command line, always relative
# to the workdir
arg = sys.argv[1]
settings_file = path.join(cwd, arg) if not path.isabs(arg) else arg
# Let's assume the settings file contains a single line,
# a path to a file we want to create
with open(settings_file, 'r') as file:
p = file.readline()
if not path.isabs(p):
# How to handle this then?
# Relative to cwd
p = path.join(cwd, p)
# vs
# Relative to the settings file (directory)
p = path.join(path.dirname(settings_file), p)
#
create_file(p)