fizyr / subst

Apache License 2.0
12 stars 5 forks source link

Enable Variable Substitutions for Windows #17

Open Schottkyc137 opened 6 months ago

Schottkyc137 commented 6 months ago

It would be nice to allow variable substitutions for windows machines as well. Since the '$' is a valid character in windows paths, and these paths use the backslash (\) character instead of the forward slash, the default substitution cannot be used. Instead, I would propose the %VARIABLE_NAME% syntax.

Note that I am not a windows user, but a user of this library and it would be nice to offer support for windows as well.

I'm happy to discuss any implementation details and can also offer to work towards enabling this feature.

de-vri-es commented 6 months ago

Hey!

We could look at making the substitution and escape character configurable. However, %FOO% is a little problematic, as it wouldn't easily allow for default value substitution and would mean a totally different parser.

For example, we could allow using %{FOO:default} and %FOO (or maybe make it configurable to disable the short form too).

I suppose in this situation it would be nicest if the escape character is also just %: %{FOO} for a substitution and %% for a literal %.

Or.. I could just add another parser that truly works like the normal Windows style, so truly %FOO%.

de-vri-es commented 6 months ago

Note that you can have variable substitution for windows paths already, you just need to be very explicit:

You would have to escape all $ and \ in the path as \$ and \\.

You can use a forward slash on Windows too, to prevent having to escape all the backslashes. Then you only need to escape the $, which is also the case on Unix systems ($ is also legal in Unix paths).

Schottkyc137 commented 6 months ago

Thanks a lot for the swift reply!

Hey!

We could look at making the substitution and escape character configurable. However, %FOO% is a little problematic, as it wouldn't easily allow for default value substitution and would mean a totally different parser.

For example, we could allow using %{FOO:default} and %FOO (or maybe make it configurable to disable the short form too).

I suppose in this situation it would be nicest if the escape character is also just %: %{FOO} for a substitution and %% for a literal %.

Or.. I could just add another parser that truly works like the normal Windows style, so truly %FOO%.

I believe that, for the benefit of your repository (which I see as a general-purpose API), both solutions would probably be nice while leaving the default what it is now (i.e., unix-style $FOO or ${FOO} replacements). Personally, I'm already more than happy if either gets implemented. For the first solution you outlined, I could then just make the escape char also configurable on our side.

Note that you can have variable substitution for windows paths already, you just need to be very explicit:

You would have to escape all $ and \ in the path as \$ and \\.

You can use a forward slash on Windows too, to prevent having to escape all the backslashes. Then you only need to escape the $, which is also the case on Unix systems ($ is also legal in Unix paths).

I see. I always thought the \ character was mandatory on windows. It's still a bit annoying when you're used to writing paths in the 'standard' way and have to escape everything.