Ticket #24993: 2-validator-set.patch

File 2-validator-set.patch, 4.1 KB (added by gaben, 5 hours ago)
  • src/org/openstreetmap/josm/data/validation/ValidatorCLI.java

    diff --git a/src/org/openstreetmap/josm/data/validation/ValidatorCLI.java b/src/org/openstreetmap/josm/data/validation/ValidatorCLI.java
    index d27be965e8..86915fb993 100644
    a b import org.openstreetmap.josm.data.projection.ProjectionRegistry;  
    3636import org.openstreetmap.josm.data.projection.Projections;
    3737import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    3838import org.openstreetmap.josm.gui.MainApplication;
     39import org.openstreetmap.josm.gui.ProgramArguments;
    3940import org.openstreetmap.josm.gui.io.CustomConfigurator;
    4041import org.openstreetmap.josm.gui.io.importexport.FileImporter;
    4142import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    … … public class ValidatorCLI implements CLIModule {  
    107108        /** --load-preferences=<url-to-xml>     Changes preferences according to the XML file */
    108109        LOAD_PREFERENCES(true, 'p'),
    109110        /** --set=<key>=<value>           Set preference key to value */
    110         SET(true, 's');
     111        SET(true, 's', OptionParser.OptionCount.MULTIPLE);
    111112
    112113        private final String name;
    113114        private final boolean requiresArgument;
    … … public class ValidatorCLI implements CLIModule {  
    467468            }
    468469            break;
    469470        case SET:
    470 
     471            final Map.Entry<String, String> keyValue = ProgramArguments.parsePreferenceToSet(argument);
     472            Config.getPref().put(keyValue.getKey(), keyValue.getValue());
     473            break;
    471474        default:
    472475            throw new AssertionError("Unexpected option: " + option);
    473476        }
  • src/org/openstreetmap/josm/gui/ProgramArguments.java

    diff --git a/src/org/openstreetmap/josm/gui/ProgramArguments.java b/src/org/openstreetmap/josm/gui/ProgramArguments.java
    index 58353d1378..6b0ec01417 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui;
    33
     4import java.util.AbstractMap;
    45import java.util.ArrayList;
    56import java.util.Arrays;
    67import java.util.Collection;
    … … public class ProgramArguments {  
    201202     */
    202203    public Map<String, String> getPreferencesToSet() {
    203204        HashMap<String, String> map = new HashMap<>();
    204         get(Option.SET).stream().map(i -> i.split("=", 2)).forEach(kv -> map.put(kv[0], getValue(kv)));
     205        get(Option.SET).stream().map(ProgramArguments::parsePreferenceToSet).forEach(e -> map.put(e.getKey(), e.getValue()));
    205206        return map;
    206207    }
    207208
     209    /**
     210     * Parses a {@code --set} argument.
     211     * @param keyValue the argument, in the form {@code key=value}
     212     * @return the key and the value. The value is empty if missing, and {@code null} if it is "null"
     213     * @since xxx
     214     */
     215    public static Map.Entry<String, String> parsePreferenceToSet(String keyValue) {
     216        String[] kv = keyValue.split("=", 2);
     217        return new AbstractMap.SimpleEntry<>(kv[0], getValue(kv));
     218    }
     219
    208220    private static String getValue(String... kv) {
    209221        if (kv.length < 2) {
    210222            return "";
  • test/unit/org/openstreetmap/josm/data/validation/ValidatorCLITest.java

    diff --git a/test/unit/org/openstreetmap/josm/data/validation/ValidatorCLITest.java b/test/unit/org/openstreetmap/josm/data/validation/ValidatorCLITest.java
    index f54343a3ff..672294b583 100644
    a b class ValidatorCLITest {  
    190190        assertEquals(Collections.singletonList("baz"), Config.getPref().getList("plugins"));
    191191    }
    192192
     193    /**
     194     * Test the --set argument
     195     */
     196    @Test
     197    void testSetPreference() {
     198        validatorCLI.processArguments(new String[]{"-s", "test.key=foo=bar", "--set=test.other=baz",
     199                "--input", "resources/styles/standard/elemstyles.mapcss"});
     200        assertEquals("foo=bar", Config.getPref().get("test.key"));
     201        assertEquals("baz", Config.getPref().get("test.other"));
     202    }
     203
    193204    /**
    194205     * Read json objects from a file
    195206     * @param path The file to read