Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2018-2020 "Graph Foundation,"
* Graph Foundation, Inc. [https://graphfoundation.org]
*
* This file is part of ONgDB.
*
* ONgDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/*
* Copyright (c) 2002-2020 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.helpers;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.function.Function;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.impl.util.Validator;
/**
* Parses a String[] argument from a main-method. It expects values to be either
* key/value pairs or just "orphan" values (w/o a key associated).
*
* A key is defined with one or more dashes in the beginning, for example:
*
*
* '-path'
* '--path'
*
*
* A key/value pair can be either one single String from the array where there's
* a '=' delimiter between the key and value, like so:
*
*
* '--path=/my/path/to/something'
*
* ...or consist of two (consecutive) strings from the array, like so:
*
* '-path' '/my/path/to/something'
*
*
* Multiple values for an option is supported, however the only means of extracting all values is be
* using {@link #interpretOptions(String, Function, Function, Validator...)}, all other methods revolve
* around single value, i.e. will fail if there are multiple.
*
* Options can have metadata which can be extracted using
* {@link #interpretOptions(String, Function, Function, Validator...)}. Metadata looks like:
*
* --my-option:Metadata my-value
*
*
* where {@code Metadata} would be the metadata of {@code my-value}.
*/
public class Args
{
private static final char OPTION_METADATA_DELIMITER = ':';
public static class ArgsParser
{
private final String[] flags;
private ArgsParser( String... flags )
{
this.flags = Objects.requireNonNull( flags );
}
public Args parse( String... arguments )
{
return new Args( flags, arguments );
}
}
public static class Option
{
private final T value;
private final String metadata;
private Option( T value, String metadata )
{
this.value = value;
this.metadata = metadata;
}
public T value()
{
return value;
}
public String metadata()
{
return metadata;
}
@Override
public String toString()
{
return "Option[" + value + (metadata != null ? ", " + metadata : "") + "]";
}
}
private static final Function> DEFAULT_OPTION_PARSER = from ->
{
int metadataStartIndex = from.indexOf( OPTION_METADATA_DELIMITER );
return metadataStartIndex == -1
? new Option<>( from, null )
: new Option<>( from.substring( 0, metadataStartIndex ), from.substring( metadataStartIndex + 1 ) );
};
private final String[] args;
private final String[] flags;
private final Map>> map = new HashMap<>();
private final List orphans = new ArrayList<>();
public static ArgsParser withFlags( String... flags )
{
return new ArgsParser( flags );
}
public static Args parse( String...args )
{
return withFlags().parse( args );
}
/**
* Suitable for main( String[] args )
* @param args the arguments to parse.
*/
private Args( String[] flags, String[] args )
{
this( DEFAULT_OPTION_PARSER, flags, args );
}
/**
* Suitable for main( String[] args )
* @param flags list of possible flags (e.g -v or -skip-bad). A flag differs from an option in that it
* has no value after it. This list of flags is used for distinguishing between the two.
* @param args the arguments to parse.
*/
private Args( Function> optionParser, String[] flags, String[] args )
{
this.flags = flags;
this.args = args;
parseArgs( optionParser, args );
}
public Args( Map source )
{
this( DEFAULT_OPTION_PARSER, source );
}
public Args( Function> optionParser, Map source )
{
this.flags = new String[] {};
this.args = null;
for ( Entry entry : source.entrySet() )
{
put( optionParser, entry.getKey(), entry.getValue() );
}
}
public String[] source()
{
return this.args;
}
public Map asMap()
{
Map result = new HashMap<>();
for ( Map.Entry>> entry : map.entrySet() )
{
Option value = Iterables.firstOrNull( entry.getValue() );
result.put( entry.getKey(), value != null ? value.value() : null );
}
return result;
}
public boolean has( String key )
{
return this.map.containsKey( key );
}
public boolean hasNonNull( String key )
{
List