Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:52:51+00:00 2026-05-25T18:52:51+00:00

I am using Apache commons CLI for command line parsing in a Scala utility

  • 0

I am using Apache commons CLI for command line parsing in a Scala utility application. One of the arguments is a database port number (--port=) that overrides the default “5432” (for PostgreSQL). I am trying to use the Option class to assist in the validation. Here is the code I came up with. Is there a better way to do the validation?

val port = Option(commandLine.getOptionValue("port", "5432")) map {
  try {
    val value = Integer.parseInt(_)
    if (value < 1 || value > 65535) throw new NumberFormatException
    value
  } catch {
    case ex: NumberFormatException =>
      throw new
        IllegalArgumentException("the port argument must be a number between 1 and 65535")
  }
} get

The port number must be an integer between 1 and 65535, inclusive.

Would it be better to do this? Why or why not?

val port = Option(commandLine.getOptionValue("port")) map {
  try {
    val value = Integer.parseInt(_)
    if (value < 1 || value > 65535) throw new NumberFormatException
    value
  } catch {
    case ex: NumberFormatException =>
      throw new
        IllegalArgumentException("the port argument must be a number between 1 and 65535")
  }
} getOrElse(5432)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:52:52+00:00Added an answer on May 25, 2026 at 6:52 pm

    I admit I’m not 100% sure, what you want to be thrown in case something goes wrong, or if the 5432 is a default port for every wrong value, but here is what I would do:

    def getPort(candidate: Option[String]) = candidate
       .map { _.toInt } // throws NumberFormatException
       .filter { v => v > 0 && v <= 65535 } // returns Option[Int]
       .getOrElse { throw new IllegalArgumentException("error message") } // return an Int or throws an exception
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Apache commons cli (1.2) for command line parsing. I have the
I am using Apache Commons IO FileUtils utility to read a file into a
I am using Apache Commons Exec on a web application in JBoss 4.2.3. Whenever
I have a java application on Websphere that is using Apache Commons FTPClient to
I'm trying to connect to a third party application API using apache commons HTTP
Using Apache's commons-httpclient for Java, what's the best way to add query parameters to
i am using the apache commons httpclient in a lotus notes java agent and
I'm using the Apache Commons EqualsBuilder to build the equals method for a non-static
I am using the Apache Commons IO: FileUtils.copyFileToDirectory(srcFile, destDir) How do I make Windows
I'm using Apache Commons Logging and SLF4J with log4j, but I also want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.