SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
Command Line Arguments
with Getopt::Long
By Ian Kluft
Part of short Lightning Talks session
Silicon Valley Perl
November 12, 2015
Santa Clara, California
Command-Line Arguments
● Many programs use command line interface
– Software builds
– Automated tools
– Experimental calculations
– Anything there isn't time to make a GUI
● Don't write new code to process CLI arguments
– Standards compliance comes with Getopt::Long
– It's in CPAN
Getopt::Long usage (1 of 2)
From the manpage:
use Getopt::Long;
      my $data   = "file.dat";
      my $length = 24;
      my $verbose;
      GetOptions ("length=i" => $length,    # numeric
                  "file=s"   => $data,      # string
                  "verbose"  => $verbose)   # flag
      or die("Error in command line argumentsn");
● Note these things:
● Getopt::Long and its function GetOptions
● Initialization of variables before using them
● Specification and destination variable pairs
Getopt::Long usage (2 of 2)
● GetOptions processes the command line
● It removes the Unix-standard CLI options
– Like --optname, --optname=value, etc
– Single dash prefixes single-character options
– These can be in any order
● It leaves everything else in @ARGV list
– Such as filenames or text
– Whatever you use them for
Option specifications (1 of 2)
● GetOptions takes specification/destination pairs
– Specification: name/format of argument
– Destination: reference to variable to store data
● Boolean
– GetOptions ( “flag” => $flag );
– Stores true (1) if --flag is present
– With “!flag”, it stores false if --noflag is present
– $flag is not modified if --flag is not present
●
So don't forget to initialize it!
Option specifications (2 of 2)
● GetOptions ( “stringarg=s” => $stringarg );
– Use “=s” for mandatory string argument
– Use “:s” for optional string argument
● GetOptions ( “integerarg=i” => $integerarg );
– Use “=i” for mandatory integer argument
– Use “:i” for optional integer argument
● GetOptions ( “floatarg=f” => $floatarg );
– Use “=f” for mandatory floating point argument
– Use “:f” for optional floating point argument
Lists from arguments
● GetOptions ("library=s" => @libfiles);
– Multiple occurrences of --library go in @libfiles
● GetOptions ("library=s@" => $libfiles);
– Same thing, but $libfiles is a reference to an array
● Example: use the argument more than once
– prog --library lib/stdlib --library lib/extlib
Hashes from arguments
● GetOptions ("define=s" => %defines);
– key=value pair is placed in %defines hash
● GetOptions ("define=s%" => $defines);
– Same thing, except $defines is a ref to a hash
● Example:
– prog --define os=linux --define vendor=redhat
Similar CPAN modules
● Getopt::LL – wider set of prcoessing rules
● Getopt::XML – read XML in Getopt::Long style
● MooseX::Getopt – Getopt for Moose
● Many other smaller modules
– Search CPAN for “Getopt”

Weitere ähnliche Inhalte

Was ist angesagt?

Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
Timo Stollenwerk
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command Line
Marcos Rebelo
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
John(Qiang) Zhang
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 

Was ist angesagt? (20)

Pyunit
PyunitPyunit
Pyunit
 
Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011 Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011
 
Perl Tidy Perl Critic
Perl Tidy Perl CriticPerl Tidy Perl Critic
Perl Tidy Perl Critic
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
 
Python Generators
Python GeneratorsPython Generators
Python Generators
 
Boost introduction
Boost introductionBoost introduction
Boost introduction
 
Perl Modules
Perl ModulesPerl Modules
Perl Modules
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command Line
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
Lua Study Share
Lua Study ShareLua Study Share
Lua Study Share
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxCNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on Linux
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
 
Android antipatterns
Android antipatternsAndroid antipatterns
Android antipatterns
 

Ähnlich wie Command Line Arguments with Getopt::Long

Internship - Final Presentation (26-08-2015)
Internship - Final Presentation (26-08-2015)Internship - Final Presentation (26-08-2015)
Internship - Final Presentation (26-08-2015)
Sean Krail
 
Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with Ruby
Nikhil Mungel
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
goccy
 
Perl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsPerl Intro 9 Command Line Arguments
Perl Intro 9 Command Line Arguments
Shaun Griffith
 
Владимир Перепелица "Модули"
Владимир Перепелица "Модули"Владимир Перепелица "Модули"
Владимир Перепелица "Модули"
Media Gorod
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
Xiaozhe Wang
 

Ähnlich wie Command Line Arguments with Getopt::Long (20)

Internship - Final Presentation (26-08-2015)
Internship - Final Presentation (26-08-2015)Internship - Final Presentation (26-08-2015)
Internship - Final Presentation (26-08-2015)
 
Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with Ruby
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
 
Perl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsPerl Intro 9 Command Line Arguments
Perl Intro 9 Command Line Arguments
 
Andriy Shalaenko - GO security tips
Andriy Shalaenko - GO security tipsAndriy Shalaenko - GO security tips
Andriy Shalaenko - GO security tips
 
Владимир Перепелица "Модули"
Владимир Перепелица "Модули"Владимир Перепелица "Модули"
Владимир Перепелица "Модули"
 
Deep learning - the conf br 2018
Deep learning - the conf br 2018Deep learning - the conf br 2018
Deep learning - the conf br 2018
 
Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginners
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
 
Devel::hdb debugger talk
Devel::hdb debugger talkDevel::hdb debugger talk
Devel::hdb debugger talk
 
Adopting GraalVM - Scale by the Bay 2018
Adopting GraalVM - Scale by the Bay 2018Adopting GraalVM - Scale by the Bay 2018
Adopting GraalVM - Scale by the Bay 2018
 
Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Go Is Your Next Language — Sergii Shapoval
Go Is Your Next Language — Sergii ShapovalGo Is Your Next Language — Sergii Shapoval
Go Is Your Next Language — Sergii Shapoval
 
Easy R
Easy REasy R
Easy R
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Debug generic process
Debug generic processDebug generic process
Debug generic process
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
 
Writing Galaxy Tools
Writing Galaxy ToolsWriting Galaxy Tools
Writing Galaxy Tools
 

Mehr von Ian Kluft

Mehr von Ian Kluft (14)

"#AprilFools Hijinks" at SVPerl April 2021 meeting
"#AprilFools Hijinks" at SVPerl April 2021 meeting"#AprilFools Hijinks" at SVPerl April 2021 meeting
"#AprilFools Hijinks" at SVPerl April 2021 meeting
 
Secure Coding in Perl
Secure Coding in PerlSecure Coding in Perl
Secure Coding in Perl
 
New Perl module Container::Buildah - SVPerl presentation
New Perl module Container::Buildah - SVPerl presentationNew Perl module Container::Buildah - SVPerl presentation
New Perl module Container::Buildah - SVPerl presentation
 
Securing a Raspberry Pi and other DIY IoT devices
Securing a Raspberry Pi and other DIY IoT devicesSecuring a Raspberry Pi and other DIY IoT devices
Securing a Raspberry Pi and other DIY IoT devices
 
Best Practices for Recovering Rocket & Balloon Payloads
Best Practices for Recovering Rocket & Balloon PayloadsBest Practices for Recovering Rocket & Balloon Payloads
Best Practices for Recovering Rocket & Balloon Payloads
 
PiFlash: Linux utility to flash SD cards for Raspberry Pi computers
PiFlash: Linux utility to flash SD cards for Raspberry Pi computersPiFlash: Linux utility to flash SD cards for Raspberry Pi computers
PiFlash: Linux utility to flash SD cards for Raspberry Pi computers
 
Code Generation in Perl
Code Generation in PerlCode Generation in Perl
Code Generation in Perl
 
Aerospace applications of Perl
Aerospace applications of PerlAerospace applications of Perl
Aerospace applications of Perl
 
Geographic Computation in Perl
Geographic Computation in PerlGeographic Computation in Perl
Geographic Computation in Perl
 
Black Rock Desert Impact Theory
Black Rock Desert Impact TheoryBlack Rock Desert Impact Theory
Black Rock Desert Impact Theory
 
Exception Handling in Perl
Exception Handling in PerlException Handling in Perl
Exception Handling in Perl
 
Geographic Computation in Perl
Geographic Computation in PerlGeographic Computation in Perl
Geographic Computation in Perl
 
Stratofox Aerospace Tracking Team presentation at Space Access 2013
Stratofox Aerospace Tracking Team presentation at Space Access 2013Stratofox Aerospace Tracking Team presentation at Space Access 2013
Stratofox Aerospace Tracking Team presentation at Space Access 2013
 
Pacificon 200905
Pacificon 200905Pacificon 200905
Pacificon 200905
 

Kürzlich hochgeladen

Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Lovely Professional University
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
Madan Karki
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
MohammadAliNayeem
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
Kamal Acharya
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 

Kürzlich hochgeladen (20)

Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
 
ChatGPT Prompt Engineering for project managers.pdf
ChatGPT Prompt Engineering for project managers.pdfChatGPT Prompt Engineering for project managers.pdf
ChatGPT Prompt Engineering for project managers.pdf
 
E-Commerce Shopping using MERN Stack where different modules are present
E-Commerce Shopping using MERN Stack where different modules are presentE-Commerce Shopping using MERN Stack where different modules are present
E-Commerce Shopping using MERN Stack where different modules are present
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor bank
 
Multivibrator and its types defination and usges.pptx
Multivibrator and its types defination and usges.pptxMultivibrator and its types defination and usges.pptx
Multivibrator and its types defination and usges.pptx
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineering
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
How to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdfHow to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdf
 

Command Line Arguments with Getopt::Long

  • 1. Command Line Arguments with Getopt::Long By Ian Kluft Part of short Lightning Talks session Silicon Valley Perl November 12, 2015 Santa Clara, California
  • 2. Command-Line Arguments ● Many programs use command line interface – Software builds – Automated tools – Experimental calculations – Anything there isn't time to make a GUI ● Don't write new code to process CLI arguments – Standards compliance comes with Getopt::Long – It's in CPAN
  • 3. Getopt::Long usage (1 of 2) From the manpage: use Getopt::Long;       my $data   = "file.dat";       my $length = 24;       my $verbose;       GetOptions ("length=i" => $length,    # numeric                   "file=s"   => $data,      # string                   "verbose"  => $verbose)   # flag       or die("Error in command line argumentsn"); ● Note these things: ● Getopt::Long and its function GetOptions ● Initialization of variables before using them ● Specification and destination variable pairs
  • 4. Getopt::Long usage (2 of 2) ● GetOptions processes the command line ● It removes the Unix-standard CLI options – Like --optname, --optname=value, etc – Single dash prefixes single-character options – These can be in any order ● It leaves everything else in @ARGV list – Such as filenames or text – Whatever you use them for
  • 5. Option specifications (1 of 2) ● GetOptions takes specification/destination pairs – Specification: name/format of argument – Destination: reference to variable to store data ● Boolean – GetOptions ( “flag” => $flag ); – Stores true (1) if --flag is present – With “!flag”, it stores false if --noflag is present – $flag is not modified if --flag is not present ● So don't forget to initialize it!
  • 6. Option specifications (2 of 2) ● GetOptions ( “stringarg=s” => $stringarg ); – Use “=s” for mandatory string argument – Use “:s” for optional string argument ● GetOptions ( “integerarg=i” => $integerarg ); – Use “=i” for mandatory integer argument – Use “:i” for optional integer argument ● GetOptions ( “floatarg=f” => $floatarg ); – Use “=f” for mandatory floating point argument – Use “:f” for optional floating point argument
  • 7. Lists from arguments ● GetOptions ("library=s" => @libfiles); – Multiple occurrences of --library go in @libfiles ● GetOptions ("library=s@" => $libfiles); – Same thing, but $libfiles is a reference to an array ● Example: use the argument more than once – prog --library lib/stdlib --library lib/extlib
  • 8. Hashes from arguments ● GetOptions ("define=s" => %defines); – key=value pair is placed in %defines hash ● GetOptions ("define=s%" => $defines); – Same thing, except $defines is a ref to a hash ● Example: – prog --define os=linux --define vendor=redhat
  • 9. Similar CPAN modules ● Getopt::LL – wider set of prcoessing rules ● Getopt::XML – read XML in Getopt::Long style ● MooseX::Getopt – Getopt for Moose ● Many other smaller modules – Search CPAN for “Getopt”