func mainParseArguments(argv []string) {
	tok := args.NewTokenizer(argv)

	for tok.Next() {
		if !tok.IsOption() {
			if conf.inputFilename.isSet() {
				fmt.Fprintf(os.Stderr, "Can't specify more than one input file.\n")
				os.Exit(1)
			}
			conf.inputFilename.set(tok.Arg())
		}

		switch tok.Arg() {
		case "-h":
			fallthrough
		case "--help":
			mainUsage(os.Stdout)
			os.Exit(0)

		case "--version":
			fmt.Printf("%s version %s\n", mainName, mainVersion)
			os.Exit(0)

		case "-o":
			fallthrough
		case "--output":
			if conf.outputFilename.isSet() {
				fmt.Fprintf(os.Stderr, "Can't specify more than one output file.\n")
				os.Exit(1)
			}
			param, err := tok.TakeParameter()
			if err != nil {
				fmt.Fprintf(os.Stderr, "Error on parsing command-line arguments: %s.\n", err.Error())
				os.Exit(1)
			}
			conf.outputFilename.set(param)
		}
	}

	if tok.Err() != nil {
		fmt.Fprintf(os.Stderr, "Error on parsing command-line arguments: %s.\n", tok.Err().Error())
		os.Exit(1)
	}
}
func mainArgs() {

	tok := args.NewTokenizer(os.Args)

	for tok.Next() {
		arg := tok.Arg()
		isOption := tok.IsOption()

		switch {
		case isOption:
			switch arg {
			case "-h", "--help":
				mainUsage(os.Stdout)
				os.Exit(0)
			case "--version":
				fmt.Printf("%s version %s\n", mainName, mainVersion)
				os.Exit(0)
			}
		case !isOption:
			if mainConfFilename != "" {
				mainUsage(os.Stderr)
				os.Exit(1)
			}
			mainConfFilename = arg
		}
	}

	if tok.Err() != nil {
		fmt.Fprintf(os.Stderr, "Error on processing arguments: %s.\n", tok.Err().Error())
		os.Exit(1)
	}

	if mainConfFilename == "" {
		mainUsage(os.Stderr)
		os.Exit(1)
	}
}
func mainArgs() {

	tok := args.NewTokenizer(os.Args)

	for tok.Next() {

		if tok.IsOption() {

			switch tok.Arg() {
			case "-h", "--help":
				fmt.Printf("Usage: %s logfile ...\n")
				os.Exit(0)

			case "--version":
				fmt.Printf("%s version %s\n", mainProgramName, mainProgramVersion)
				os.Exit(0)

			case "-d":
				param, err := tok.TakeParameter()
				if err != nil {
					fmt.Fprintf(os.Stderr, "Failed to take parameter to '%s' option: %S", tok.Arg(), err.Error())
					os.Exit(1)
				}

				mainConfLogDirectory = param

			case "-a":
				param, err := tok.TakeParameter()
				if err != nil {
					fmt.Fprintf(os.Stderr, "Failed take parameter to '%s' option: %s", tok.Arg(), err.Error())
					os.Exit(1)
				}

				namenetwork := strings.Split(param, ":")
				if len(namenetwork) != 2 {
					fmt.Fprintf(os.Stderr, "'%s' is not a valid name-network association.\n", param)
					os.Exit(1)
				}
				name := namenetwork[0]
				network := namenetwork[1]

				names, ok := mainConfNetworkNames[network]
				if !ok {
					names = []string{}
				}
				mainConfNetworkNames[network] = append(names, name)

			default:
				fmt.Fprintf(os.Stderr, "Unrecognized option, '%s'.\n", tok.Arg())
				os.Exit(1)

			}

		} else {
			namenetwork := strings.Split(tok.Arg(), "@")
			if len(namenetwork) != 2 {
				fmt.Fprintf(os.Stderr, "'%s' is not a valid channel", tok.Arg())
				os.Exit(1)
			}
			name := namenetwork[0]
			network := namenetwork[1]
			mainConfChannels = append(mainConfChannels, mainChannel{name: name, network: network})
		}

	}

	if tok.Err() != nil {
		fmt.Fprintf(os.Stderr, "Error occurred on processing command-line arguments: %s.\n", tok.Err().Error())
		os.Exit(1)
	}

	if len(mainConfChannels) == 0 {
		fmt.Fprintf(os.Stderr, "No channels specified.\n")
		os.Exit(1)
	}
}
func mainParseArgs() {
	defer func() {
		err, isErr := recover().(error)
		if isErr {
			fmt.Fprintf(os.Stderr, "Error on processing command-line arguments: %s.\n", err.Error())
			os.Exit(1)
		}
	}()

	tok := args.NewTokenizer(os.Args)

	channelStrings := []string{}

	for tok.Next() {
		arg := tok.Arg()
		isOption := tok.IsOption()

		if !isOption {
			channelStrings = append(channelStrings, arg)
			continue
		}

		// TODO: Make username and nickname options

		switch arg {
		case "-h", "--help":
			mainPrintUsage(os.Stdout)
			os.Exit(0)
		case "--version":
			fmt.Printf("%s version %s\n", mainName, mainVersion)
			os.Exit(0)
		case "-t", "--timeout":
			param, err := tok.TakeParameter()
			if err != nil {
				panic(err)
			}
			secs, err := strconv.ParseFloat(param, 64)
			if err != nil {
				panic(fmt.Errorf("Failed to parse parameter to %s, %s", arg, param))
			}
			confTimeout = time.Duration(float64(time.Second) * secs)
		case "-m", "--query-method":
			param, err := tok.TakeParameter()
			if err != nil {
				panic(err)
			}
			m, ok := query.GetMethodByName(param)
			if !ok {
				panic(fmt.Errorf("Not a recognized query method, '%s'", param))
			}
			confQueryMethods = append(confQueryMethods, m)
		case "--debug-irc":
			confDebugIRC = true
		default:
			panic(fmt.Errorf("Unexpected option, '%s'", arg))
		}
	}

	if tok.Err() != nil {
		panic(tok.Err())
	}

	for _, v := range channelStrings {
		div := strings.Split(v, "@")
		if len(div) != 2 {
			panic(fmt.Errorf("Invalid channel string '%s'", v))
		}

		// TODO: Validate and canonicalize channel and server names
		server := div[1]
		channel := div[0]

		serverChannels, ok := confChannels[server]
		if !ok {
			serverChannels = []string{}
		}
		serverChannels = append(serverChannels, channel)
		confChannels[server] = serverChannels
	}

	if len(confQueryMethods) == 0 {
		confQueryMethods = append(confQueryMethods, query.ListMethod)
	}
}