Example #1
0
int main(int argc, char *argv[])
{
	struct libscols_table *tb;
	int c, notree = 0, nstart = -1, nend = -1;


	static const struct option longopts[] = {
		{ "ascii",	0, 0, 'i' },
		{ "csv",        0, 0, 'c' },
		{ "list",       0, 0, 'l' },
		{ "noheadings",	0, 0, 'n' },
		{ "pairs",      0, 0, 'p' },
		{ "json",       0, 0, 'J' },
		{ "raw",        0, 0, 'r' },
		{ "range-start",1, 0, 'S' },
		{ "range-end",  1, 0, 'E' },
		{ NULL, 0, 0, 0 },
	};

	setlocale(LC_ALL, "");	/* just to have enable UTF8 chars */

	scols_init_debug(0);

	tb = scols_new_table();
	if (!tb)
		err(EXIT_FAILURE, "failed to create output table");

	while((c = getopt_long(argc, argv, "ciJlnprS:E:", longopts, NULL)) != -1) {
		switch(c) {
		case 'c':
			scols_table_set_column_separator(tb, ",");
			scols_table_enable_raw(tb, 1);
			notree = 1;
			break;
		case 'i':
			scols_table_enable_ascii(tb, 1);
			break;
		case 'J':
			scols_table_set_name(tb, "scolstest");
			scols_table_enable_json(tb, 1);
			break;
		case 'l':
			notree = 1;
			break;
		case 'n':
			scols_table_enable_noheadings(tb, 1);
			break;
		case 'p':
			scols_table_enable_export(tb, 1);
			notree = 1;
			break;
		case 'r':
			scols_table_enable_raw(tb, 1);
			notree = 1;
			break;
		case 'S':
			nstart = strtos32_or_err(optarg, "failed to parse range start") - 1;
			break;
		case 'E':
			nend = strtos32_or_err(optarg, "failed to parse range end") - 1;
			break;
		default:
			usage(stderr);
		}
	}

	scols_table_enable_colors(tb, 1);
	setup_columns(tb, notree);

	if (optind == argc)
		add_lines(tb, ".");
	else while (optind < argc)
		add_lines(tb, argv[optind++]);

	if (nstart >= 0 || nend >= 0) {
		/* print subset */
		struct libscols_line *start = NULL, *end = NULL;

		if (nstart >= 0)
			start = scols_table_get_line(tb, nstart);
		if (nend >= 0)
			end = scols_table_get_line(tb, nend);

		if (start || end)
			scols_table_print_range(tb, start, end);
	} else
		/* print all table */
		scols_print_table(tb);

	scols_unref_table(tb);
	return EXIT_SUCCESS;
}
Example #2
0
int main(int argc, char *argv[])
{
	struct libscols_table *tb;
	int c, notree = 0, clonetb = 0;

	static const struct option longopts[] = {
		{ "help",       0, 0, 'h' },
		{ "noheadings",	0, 0, 'n' },
		{ "list",       0, 0, 'l' },
		{ "ascii",	0, 0, 'i' },
		{ "pairs",      0, 0, 'p' },
		{ "clone",      0, 0, 'C' },
		{ "csv",        0, 0, 'c' },
		{ NULL, 0, 0, 0 },
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	scols_init_debug(0);

	tb = scols_new_table();
	if (!tb)
		err(EXIT_FAILURE, "faild to create output table");

	while((c = getopt_long(argc, argv, "nlirpCc", longopts, NULL)) != -1) {
		switch(c) {
		case 'h':
			usage(stdout);
			break;
		case 'l':
			notree = 1;
			break;
		case 'n':
			scols_table_enable_noheadings(tb, 1);
			break;
		case 'p':
			scols_table_enable_export(tb, 1);
			notree = 1;
			break;
		case 'i':
			scols_table_enable_ascii(tb, 1);
			break;
		case 'r':
			scols_table_enable_raw(tb, 1);
			notree = 1;
			break;
		case 'c':
			scols_table_set_column_separator(tb, ",");
			scols_table_enable_raw(tb, 1);
			notree = 1;
			break;
		case 'C':
			clonetb = 1;
		default:
			usage(stderr);
		}
	}

	scols_table_enable_colors(tb, 1);
	set_columns(tb, notree);

	while (optind < argc)
		add_lines(tb, argv[optind++]);

	if (clonetb) {
		struct libscols_table *xtb = scols_copy_table(tb);

		scols_print_table(xtb);
		scols_unref_table(xtb);
	} else
		scols_print_table(tb);

	scols_unref_table(tb);

	return EXIT_SUCCESS;
}