<commit_before>package se.culvertsoft.mgen.javapack.serialization;

import java.io.IOException;

import se.culvertsoft.mgen.api.model.BoolType;
import se.culvertsoft.mgen.api.model.EnumType;
import se.culvertsoft.mgen.api.model.Field;
import se.culvertsoft.mgen.api.model.StringType;
import se.culvertsoft.mgen.javapack.classes.ClassRegistryBase;
import se.culvertsoft.mgen.javapack.classes.MGenBase;
import se.culvertsoft.mgen.javapack.exceptions.SerializationException;

public class CommandLineArgParser<T extends MGenBase> {

	private final Class<T> m_cls;
	private final ClassRegistryBase m_classRegistry;
	private final Field[] m_fields;
	private final JsonReader m_jsonReader;
	private final StringBuilder m_builder = new StringBuilder();
	private int m_n = 0;

	public CommandLineArgParser(final Class<T> cls, final ClassRegistryBase classRegistry) {
		m_cls = cls;
		m_classRegistry = classRegistry;
		m_fields = newInstance()._fields();
		m_jsonReader = new JsonReader(m_classRegistry);
	}

	public T parse(final String[] args) throws IOException {
		m_builder.setLength(0);

		m_builder.append("{");
		m_n = 0;

		for (int i = 0; i < args.length; i++) {
			final Field field = findField(prune(args[i]));
			final String value = i + 1 < args.length ? prune(args[i + 1]) : null;

			if (field.typ() == BoolType.INSTANCE) {
				addField(field, "true");
			} else {
				if (value == null)
					throw new SerializationException("No value provided for field " + field);
				addField(field, value);
				i++;
			}
		}

		m_builder.append("}");

		System.out.println("Converted cmd line args to: " + m_builder.toString());

		return m_jsonReader.readObject(m_builder.toString(), m_cls);
	}

	private T newInstance() {
		try {
			return m_cls.newInstance();
		} catch (final Exception e) {
			throw new SerializationException("Unable to create instance of class " + m_cls, e);
		}
	}

	private String unQuote(final String txtIn) {
		String src = txtIn.trim();
		if (src.startsWith("\"")) {
			src = src.substring(1);
		}
		if (src.endsWith("\"")) {
			src = src.substring(0, src.length());
		}
		return src;
	}

	private String prune(final String txt) {
		return removeMinus(unQuote(txt.trim()).trim());
	}

	private String removeMinus(String txt) {
		return txt.startsWith("-") ? txt.substring(1) : txt;
	}<commit_msg>fixed arg parser for long args<commit_after>package se.culvertsoft.mgen.javapack.serialization;

import java.io.IOException;

import se.culvertsoft.mgen.api.model.BoolType;
import se.culvertsoft.mgen.api.model.EnumType;
import se.culvertsoft.mgen.api.model.Field;
import se.culvertsoft.mgen.api.model.StringType;
import se.culvertsoft.mgen.javapack.classes.ClassRegistryBase;
import se.culvertsoft.mgen.javapack.classes.MGenBase;
import se.culvertsoft.mgen.javapack.exceptions.SerializationException;

public class CommandLineArgParser<T extends MGenBase> {

	private final Class<T> m_cls;
	private final ClassRegistryBase m_classRegistry;
	private final Field[] m_fields;
	private final JsonReader m_jsonReader;
	private final StringBuilder m_builder = new StringBuilder();
	private int m_n = 0;

	public CommandLineArgParser(final Class<T> cls, final ClassRegistryBase classRegistry) {
		m_cls = cls;
		m_classRegistry = classRegistry;
		m_fields = newInstance()._fields();
		m_jsonReader = new JsonReader(m_classRegistry);
	}

	public T parse(final String[] args) throws IOException {
		m_builder.setLength(0);

		m_builder.append("{");
		m_n = 0;

		for (int i = 0; i < args.length; i++) {
			final Field field = findField(prune(args[i]));
			final String value = i + 1 < args.length ? prune(args[i + 1]) : null;

			if (field.typ() == BoolType.INSTANCE) {
				addField(field, "true");
			} else {
				if (value == null)
					throw new SerializationException("No value provided for field " + field);
				addField(field, value);
				i++;
			}
		}

		m_builder.append("}");

		System.out.println("Converted cmd line args to: " + m_builder.toString());

		return m_jsonReader.readObject(m_builder.toString(), m_cls);
	}

	private T newInstance() {
		try {
			return m_cls.newInstance();
		} catch (final Exception e) {
			throw new SerializationException("Unable to create instance of class " + m_cls, e);
		}
	}

	private String unQuote(final String txtIn) {
		String src = txtIn.trim();
		if (src.startsWith("\"")) {
			src = src.substring(1);
		}
		if (src.endsWith("\"")) {
			src = src.substring(0, src.length());
		}
		return src;
	}

	private String prune(final String txt) {
		return removeMinuses(unQuote(txt.trim()).trim());
	}

	private String removeMinuses(String txt) {
		while (txt.startsWith("-")) {
			txt = txt.substring(1);
		}
		return txt;
	}
{'fraction_non_alphanumeric': 0.1298313047191971, 'fraction_numerical': 0.003843689942344651, 'mean_word_length': 4.334851936218679, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878040', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1564, 'n_words': 367}